0

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"

SST
  • 443
  • 3
  • 18
  • 35
  • 1
    doubled question http://stackoverflow.com/questions/1422629/jquery-object-doesnt-support-this-property-or-method-in-internet-explorer – Anatoly Aug 05 '11 at 08: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 Aug 05 '11 at 09:04
  • possible duplicate of [Get All Elements in an HTML document with a specific CSS Class](http://stackoverflow.com/questions/210377/get-all-elements-in-an-html-document-with-a-specific-css-class) – Felix Kling Aug 05 '11 at 10:43

4 Answers4

2

Because you just made that method up!

You want document.getElementsByClassName: https://developer.mozilla.org/en/DOM/document.getElementsByClassName

Evan Trimboli
  • 29,459
  • 5
  • 43
  • 65
2

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")
troelskn
  • 111,113
  • 23
  • 130
  • 153
1

Via pure javascript:
You can use the getElementsByClassName from this question

With jQuery, this is lot more simple, use the class selector:

$(".className)
Community
  • 1
  • 1
Nivas
  • 17,720
  • 4
  • 58
  • 75
1
  1. There is no function like getElementByClass.

  2. 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"
Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
Benz
  • 103
  • 6