3

Question is simple. Not using jQuery... how do I get the value of class value in a DIV using regular JavaScript?

Thanks.

1
  • Should've posted at least a little HTML to make your post more clear. Commented Feb 22, 2017 at 20:31

2 Answers 2

13

Assuming this HTML

<div id="Target" class="MyClass" />

Like This

var ElementCssClass = document.getElementById("Target").className;
Sign up to request clarification or add additional context in comments.

Comments

10

get a reference to the div and use .className

document.getElementById('someDiv').className

5 Comments

+1 @Jahkr: Or if not getElementById, it might be from the list returned by getElementsByTagName, or you might navigate to it from another element via childNodes, parentNode, etc. Details: w3.org/TR/2000/REC-DOM-Level-2-Core-20001113 | w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109 | w3.org/TR/DOM-Level-3-Core
Very odd that the OP accepted the other answer. I guess it was having both the HTML and the JavaScript that made it more useful to them, showed more clearly how they linked up.
especially since the original answer was "use getAttribute"... I dislike edits on here sometimes.
@cwolves: Actually, that was a different answer, which the answerer deleted after getting repeatedly downvoted. (It was pointed out to him that it didn't work, and he said "okay, thanks" and then left the answer as it was, didn't fix it, didn't delete it. Hence the downvotes.) Mimisbrunnr said className right away.
@cwolves: I should explain that it's not that I have an obsession and/or incredible memory. There's a rep level where you can see deleted answers. :-)

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.