I am trying to change the class of a element with Javascript using the below code:
parent.document.getElementById('<?php echo $markid ?>').class = 'unlistened';
Not having much luck though. How do I do this properly?
I am trying to change the class of a element with Javascript using the below code:
parent.document.getElementById('<?php echo $markid ?>').class = 'unlistened';
Not having much luck though. How do I do this properly?
Use .className
instead of .class
Class is a reserved word in JS so it's changed to className. A few other HTML attributes are changed in a similar way. "for" changes to "htmlFor" for instance.
Just a tip, it's much easier to use jQuery if you're doing JavaScript these days, since it's so much easier and only really requires you to have good CSS knowledge (for selectors and so on)
You'd do $(selector).addClass('myclass');
And retrive it like so:
$(selector).attr('class');
Oh yes, and please note if you're using a mixture of classes on any element, the above function will return them all delimited by a space (like you'd define in the HTML class attribute.) If you want to check if a particular class exists, do this:
$(selector).hasClass('class');
That's the basics of class manipulation with jQuery. Check out http://docs.jquery.com/ for the rest.