get element by class jquery code example

Example 1: javascript get element by class

var elementsArray = document.getElementsByClassName("someclassname");

Example 2: find class using jquery

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

Example 3: jquery class selector

$('.class').jquery_function();

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: how to find a name of class from page in jquery

$("input[type='submit']").click(function() {
    if ($(".Mandatory").length > 0) {
        // Do some code here
    }
});

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

if ($('.Mandatory').length > 0) {
  //do your thing
}

Tags:

Css Example