How to find particular class exists on a page using JQuery How to find particular class exists on a page using JQuery jquery jquery

How to find particular class exists on a page using JQuery


Just check how many elements you hit, when you search for it with 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();    }); }

EDITIf you wanna do that check for your submit button click, just do that check after the click:

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


If you want to only do the action once, you could use:

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

Otherwise if you want to do it for each Mandatory element:

$('.Mandatory').each(function(){  //do your thing});


The best way to do this is search class within the body tag.

$('body').find('.Mandatory').length;