tags:

views:

74

answers:

2

Well I already saw this question here. But m a little bit of confused. There were several solutions provided:-

  1. var div1Class = $('#div1').attr('className');

    This one was the accepted solution with 2 up votes.

  2. var divClass = $("#div1").attr("class")

    This got an up vote too.

Which is the correct one? And if both are correct, what is the difference between the two?

+3  A: 

There is no HTML attribute className - the correct code is:

var divClass = $("#div1").attr("class");

className is a Javascript DOM property, so if you weren't using jQuery you could do:

var divClass = document.getElementById('div1').className;
Mark B
well, className option was the accepted solution on the link to the question I provided !!
Manish
I have always used 'class', but yes, it seems najmeddine is right in his answer too (or actually, we are both right): http://www.nabble.com/class-or-className-td5219071s27240.html
Mark B
Yes, I guess he's more correct..!! :-) So marking his answer as the solution
Manish
Yep fair enough :)
Mark B
+5  A: 

it's permissible to use 'class', jQuery will fix it, but jQuery recommands to use 'className' Attributes/attr

to avoid collision with classes definition, DOM renamed 'class' attribute to 'className'. same thing for the 'for' attribute that was renamed 'htmlFor'. (see here :1.6.2. Naming Exceptions)

najmeddine