Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get class name from tr tag - 'undefined'

I need to get the class name from a given <tr> tag, but am unable to do so.

<table cellpadding=5 cellspacing=5>
    <tr id='cat_abc123' class='class_a'>
        <td>foo</td>
        <td><input type='checkbox' name='cb1' value='1' onClick="info(this, 'abc123')">
    </tr>
</table>

 <script language='javascript'>
     function info(theElement, id)
     {
         tr_id = 'cat_' + id;
         alert(tr_id + ' ' + document.getElementById(tr_id).class);
     }
 </script>

Working example: http://jsfiddle.net/rQpeu/

What am I missing?

Update

I was using the wrong descriptor - should use Classname. Thanks for the prompt responses everyone! Updated jsfiddle: http://jsfiddle.net/rQpeu/3/

like image 355
a coder Avatar asked Dec 06 '25 09:12

a coder


1 Answers

element.class is incorrect.

You need to use element.className.

https://developer.mozilla.org/en-US/docs/DOM/element.className

like image 134
onlywei Avatar answered Dec 08 '25 22:12

onlywei