privateer35 privateer35 - 1 year ago 66
Javascript Question

Store the n class declaration from an HTML tag in Javascript

I have dynamically generated

that receives two generated classes applied to it. For example, I have :

<div class="container">...</div>

Then after I mess with it using some Javascript, it becomes:

<div class="container post post-hello-world">...</div>

For this example how can I grab the third class (
) and store it somewhere to be used for something else in my Javascript code using pure Javascript?

Answer Source

Use classList and item if the class you want is always third:

var className = document.getElementsByClassName('container')[0].classList.item(2);



Try before buy

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download