-2

I know this question was posted many times but I can't understand my problem :

When I do :

alert($('#CalendarType').className);

I keep getting undefined in the alert, at start by searching on the web I thought it was because my div wasn't already load but I tried something else :

alert($('#CalendarType').html());

And this work.... I can't understand . If someone can explain me what I did wrong

BleuBizarre
  • 368
  • 2
  • 15
  • "className gets and sets the value of the class attribute of the specified element." https://developer.mozilla.org/en-US/docs/Web/API/Element.className. You need "Gets or sets the element's identifier (attribute id)." https://developer.mozilla.org/en-US/docs/Web/API/Element.id. – loveNoHate Sep 14 '14 at 09:03
  • `$('#CalendarType')[0].className` – elclanrs Sep 14 '14 at 09:04

2 Answers2

2

$(someSelector) returns a jQuery object, not a DOM object.

className is a property of a DOM object, not of a jQuery object.

You can either extract the DOM object from the jQuery object:

alert($('#CalendarType')[0].className);

Or use the jQuery attr method:

alert($('#CalendarType').attr('class'));
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

You can use .attr() function instead. I am sure this will work:

alert($('#CalendarType').attr("class"));
kecer
  • 3,031
  • 2
  • 21
  • 24