Given a webpage that has a div such as:
<div id="test" class="test">something</div>
Is it possible to insert additional class names into the list, so that it read something like:
<div id="test" class="test test1 test2">something</div>
? I could easily change the text between the divs using innerHTML, but can’t figure out a way to add to the class list.
What you want to do is invalid (An element cannot have more than on ID). An ID can’t contain spaces either, however Chrome seems to allow it. I’m not sure about other browsers, but in Chrome you could do something like this to update it:
But then to access that element you’ll have to use:
Keep in mind that this is invalid so there’s no reason that it should continue to work in future versions of a browser just because it works now.
Edit
You could use these Javascript functions I wrote the other day for accessing adding and removing classes. The addClass function will accept an array, or array-like object and add the class to each element. If it’s not array-like it will just add the class to that element. Use it like:
Code:
Minified version: