privateer35 privateer35 - 1 year ago 43
HTML Question

Store the nth class name from a 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