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
Post a Comment