Get div id from its class name

How can I find id based on its class just using javascript. I know this easily with jQuery , what is the solution .. using getElementsByTagName?

+11
javascript
source share
2 answers
document.getElementsByClassName('myClassName')[0].id 

or

 document.querySelector('.myClassName').id 
+26
source share

The first step would be to find the element with the given class name. There are currently some features supported by modern browsers, such as the getElementsByClassName and querySelector . but they are not cross browser solutions.

That is, getElementsByClassName not supported by IE 6-8, and querySelector not supported by IE6-7 & FF3
source: http://www.quirksmode.org/dom/w3c_core.html

Therefore, if you do not support these browsers, you can use them, otherwise you will need a js shell function similar to the one mentioned in this post originally found on justswell.org .

+1
source share

All Articles