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;
- javascript case-sensitive. property
onclick
(but should using addeventlistener anyway). - putting
()
on end of function name call function. want assign property. remove parenthesis.
Comments
Post a Comment