Search code examples
jquery

How to find particular class exists on a page using JQuery


I want to write JQuery which will find whether Class="Mandatory" exists on the page and if any element is having this class then only perform certain action.

Thanks


Solution

  • 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();
        }); 
    }
    

    EDIT If 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
        }
    });