Using jQuery, how can I get the current class of a div called div1
?
Just get the class attribute:
var div1Class = $('#div1').attr('class');
Example
<div id="div1" class="accordion accordion_active">
To check the above div for classes contained in it
var a = ("#div1").attr('class');
console.log(a);
console output
accordion accordion_active
$('#div1').attr('class')
will return a string of the classes. Turn it into an array of class names
var classNames = $('#div1').attr('class').split(' ');
From now on is better to use the .prop() function instead of the .attr() one.
Here the jQuery documentation:
As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.
var div1Class = $('#div1').prop('class');
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'
$("#div1").attr("class")
try this
$("#div1").attr("class")