James
James

Reputation: 1706

Check page and get form names and match them up

I am new to jQuery/JavaScript and I am wondering if it's possible to get form names on a page and match them up and execute code depending if they're found.

Now thinking about it, what I am doing might not work and might explain why it's not.

$(document).ready(function() {
    var formName = $('form').attr('name');
    if(formName == "carts_list_form"){
        console.log("form loaded");
    }
});

HTML

<form action="/admin.php" method="post" target="" name="carts_list_form" class="cm-processed-form cm-check-changes">

Upvotes: 2

Views: 69

Answers (1)

gre_gor
gre_gor

Reputation: 6771

If you have multiple forms in your page $('form').attr('name') will only return the name of the first one.

If you want to check for the existence of a form with a specific name use:

if ($("form[name='carts_list_form']").length)
{
    console.log("form loaded");
}

Upvotes: 2

Related Questions