jquery get element class code example

Example 1: jquery get class name

//jQuery get class name of element 
var className=$('#myElementID').attr('class');

Example 2: jquery: get class list using element id

$('#elementID').prop('classList').add('yourClassName')
$('#elementID').prop('classList').remove('yourClassName')

Example 3: jquery find tag and class

$(".txtClass")                  =>  getElementsByClassName()

$("#childDiv2 .txtClass")       =>  getElementById(),
                                    then getElementsByClassName()

$("#childDiv2 > .txtClass")     =>  getElementById(),
                                    then iterate over children and check class

$("input.txtClass")             =>  getElementsByTagName(),
                                    then iterate over results and check class

$("#childDiv2 input.txtClass")  =>  getElementById(),
                                    then getElementsByTagName(),
                                    then iterate over results and check class

Example 4: how to get class name in jquery

var className = $('#sidebar').attr('class');