Page 1 of 1

JavaScript - get element's class

Posted: Fri Oct 06, 2006 4:09 am
by Oren
I really don't know JavaScript but I'm trying to edit some extension for Vanilla so I need some help, thanks in advance :wink:

Let's say that this is some part from my source:

Code: Select all

<li id="my_id" class="my_class">
Now, I have a JavaScript variable targetId which holds the name of the element's id. So: targetId = 'my_id'.
How can I get, let's say into another variable, the class of the element which its id is held by targetId?

Something like this:

Code: Select all

var my_class = some_made_up_function(targetId);
After the above line, my_class should hold the string 'my_class'.

Posted: Fri Oct 06, 2006 7:23 am
by aaronhall
Maybe this will work (not tested):

Code: Select all

className = document.getElementById('myElement').getAttribute('class')

Posted: Fri Oct 06, 2006 9:54 am
by Oren
Oh, thank you so much aaronhall! :D

Posted: Fri Oct 06, 2006 12:13 pm
by n00b Saibot

Code: Select all

elemClass = document.getElementById('myElement').className