jquery find by class code example

Example 1: get all classes of element jquery

var classList = $('#divId').attr('class').split(/\s+/);
$.each(classList, function(index, item) {
    if (item === 'someClass') {
        //do something
    }
});

Example 2: find class using jquery

var myVar = $("#start").find('.myClass').val();

Example 3: jquery: get class list using element id

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

Example 4: 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 5: jquery find

$( "li.item-ii" ).find( "li" ).css( "background-color", "red" );

Example 6: how to find a name of class from page in jquery

if ($(".Mandatory").length > 0) {
    // Do stuff with $(".Mandatory")
    $(".Mandatory").each(function() {
        // "this" points to current item in looping through all elements with
        // class="Mandatory"
        $(this).doSomejQueryWithElement();
    }); 
}