How to check class name using javascript?? I am using this statement.. if (document.getElementByClass("expandable")
but it give me error.. "Object doesn't support this property or method"
How to check class name using javascript?? I am using this statement.. if (document.getElementByClass("expandable")
but it give me error.. "Object doesn't support this property or method"
Share Improve this question asked Aug 5, 2011 at 8:56 SSTSST 4595 gold badges20 silver badges35 bronze badges 3- 1 doubled question stackoverflow./questions/1422629/… – Anatoly Commented Aug 5, 2011 at 8:59
- if you want to check class name of some element - document.getElementById('myid').className. What you are trying to do is get elements that have some class as it seems to me... – Gatekeeper Commented Aug 5, 2011 at 9:04
- possible duplicate of Get All Elements in an HTML document with a specific CSS Class – Felix Kling Commented Aug 5, 2011 at 10:43
4 Answers
Reset to default 2Because you just made that method up!
You want document.getElementsByClassName: https://developer.mozilla/en/DOM/document.getElementsByClassName
There is no function document.getElementByClass
. Either check for the property .className
of potential objects, or use a higher level framework which implements this (Such as jquery). This is the syntax with jquery:
$(".expandable")
Via pure javascript:
You can use the getElementsByClassName
from this question
With jQuery, this is lot more simple, use the class selector:
$(".className)
There is no function like
getElementByClass
.You have to use
document.getElementsByClassName
; it will return an array so you have to use loop to get values.
But MSIE will not support this function so you have to check your class name like:
((document.getElementsByTagName("div"))[i].className)=="yourclassname"