views:

471

answers:

6

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
is that really an attribute? `className`..? Isn't `className` only used for javascript? `document.getElementById("div1").className`
peirix
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
A: 
$("#div1").attr("class")
Tzury Bar Yochay
+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
A: 
var classname=$('#div1').attr('class')
halocursed
A: 

try this

$("#div1").attr("class")

Ravi
+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