Wednesday, 15 May 2013

My Javascript isn't changing className property -



My Javascript isn't changing className property -

here main part of code attempting toggle class on p tag (info) on click of button (btn). i'm not sure i'm doing wrong. have never used classname property before i'm not sure if i'm missing simple, or if there fundamental error in logic trying effort this.

var handler = function(event){ if(info.classname === 'on'){ info.classname='off'; }else{ info.classname='on'; } }; btn.addeventlistener('click', handler);

your code works fine. can run snippet below see works.

class="snippet-code-js lang-js prettyprint-override">var btn = document.getelementbyid("btn"), info = document.getelementbyid("info"); var handler = function (event) { if (info.classname === 'on') { info.classname = 'off'; } else { info.classname = 'on'; } }; btn.addeventlistener('click', handler); class="snippet-code-css lang-css prettyprint-override">.on { background-color: red; } .off { background-color: green; } class="snippet-code-html lang-html prettyprint-override"><button id=btn>button</button> <p id=info>info</p>

javascript javascript-events classname

No comments:

Post a Comment