javascript - JS Change class of element -


all i'm trying toggle class of elements id when clicked on.

<script>  function selectload(id) {     if(document.getelementbyid(id).classname == "load") {         document.getelementbyid(id).classname = "loadselected";     } else {         document.getelementbyid(id).classname = "load";     } }  </script>  <div class="load" id=" ... set php per element ... " onclick="selectload(this)"> 

this returns error...

uncaught typeerror: cannot read property 'classname' of null

so guess doesn't conditional.

i'm new js , need help, in advance.

you passing dom element (using this), not id.

you should change code to

function selectload(element) {     if (element.classname == "load") {         element.classname = "loadselected";     } else {         element.classname = "load";     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -