javascript - onClick event is not triggering function -


i have following script attempts change color of div, when clicked, id of wrapper. have tried variations of what's below, can't see issue. on click event not trigger function. have tried changing background-color backgroundcolor, didn't make difference. know i'm using global variable here, please ignore part:

var wrapper;  function wrappercolortocoral () {     wrapper.setattribute('style', 'background-color:lightcoral;'); }  function wrappercolortogreen () {     wrapper.setattribute('style', 'background-color:lightgreen;'); }  function colorchange () {     //if (wrapper.getattribute('style', 'background-color:lightcoral;') === true) {     if (wrapper.style != 'background-color:lightgreen;') {     wrappercolortogreen();     }     else {     wrappercolortocoral();     } } // init function function init () {     wrapper = document.getelementbyid('wrapper');     wrapper.onclick = colorchange(); }  window.onload = init; 

thank help

edit (working - quentin):

var wrapper  function wrappercolortocoral () {     wrapper.style.backgroundcolor="lightcoral"; }  function wrappercolortogreen () {     wrapper.style.backgroundcolor="lightgreen"; }  function colorchange () {     if (wrapper.style.backgroundcolor==="lightcoral") {     wrappercolortogreen();     }     else {     wrappercolortocoral();     } }  function init () { wrapper = document.getelementbyid('wrapper'); wrapper.addeventlistener("click", colorchange, false); } window.onload = init; 

  1. javascript case-sensitive. property onclick (but should using addeventlistener anyway).
  2. putting () on end of function name call function. want assign property. remove parenthesis.

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -