Reputation: 1107
In a HTML page, if I type in my console $("#title")[0]
, I get:
<div id="title" class="ms-font-xxl ms-fontColor-neutralSecondary ms-fontWeight-semilight">Change Case</div>
Ok. If I type $("#title")[0].id
, I get "title"
.
The problem is when I type $("#title")[0].class
, I get undefined
.
Shouldn't I get ms-font-xxl ms-fontColor-neutralSecondary ms-fontWeight-semilight
? What did I do wrong?
Upvotes: 1
Views: 77
Reputation: 11342
Two way of getting it:
Before jQuery 1.6, use .attr()
After jQuery 1.6+, use .prop()
jQuery method .prop()
: $('#title').prop('class')
JS method .className
: $('#title')[0].className
.prop
REF: http://api.jquery.com/prop/
Element.className
REF: https://developer.mozilla.org/en-US/docs/Web/API/Element/className
console.log($('#title').prop('class'));
console.log($('#title')[0].className);
//get array of class name
console.log($('#title')[0].className.split(/\s+/));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="title" class="ms-font-xxl ms-fontColor-neutralSecondary ms-fontWeight-semilight">Change Case</div>
Upvotes: 5
Reputation: 1794
Try
$('#title').attr('class')
Not all the attributes could be grabbed instantly
Upvotes: 1