Using jquery ,how can i get the current css class of a div called "div1" ?
+3
A:
Just get the className attribute:
var div1Class = $('#div1').attr('className');
CMS
2009-10-08 07:23:31
is that really an attribute? `className`..? Isn't `className` only used for javascript? `document.getElementById("div1").className`
peirix
2009-10-08 10:15:12
Saw now that according to the jQuery doc it's suggested to use `className` so it won't interfere with the reserved word `class` in javascript...
peirix
2009-10-08 10:29:07
+2
A:
$('#div1').attr('class')
will return a string of the classes. Turn it into an array of class names
var classNames = $('#div1').attr('class').split(' ');
Russ Cam
2009-10-08 07:24:26
+4
A:
Simply by
var divClass = $("#div1").attr("class")
You can do some other stuff to manipulate element's class
$("#div1").addClass("foo"); // add class 'foo' to div1
$("#div1").removeClass("foo"); // remove class 'foo' from div1
$("#div1").toggleClass("foo"); // toggle class 'foo'
Darth
2009-10-08 07:32:38