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"

3

4 Answers 4

2

Because you just made that method up!

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

Sign up to request clarification or add additional context in comments.

1 Comment

Note that this method is relatively new, and thus not supported on older browsers.
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")

Comments

1

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

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

$(".className)

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.