Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the ID of a form?

Tags:

jquery

closest

I'm using the following code to attach an event to the submit button of a form :

$('form.manage-users-form').on('click','button.special',function() {

    if ($(this).hasClass('selected')){
        console.log('This user is already special');
    } else {

        $(this).parent().find('button').removeClass('selected');
        $(this).addClass('selected');

        var user_id = $(this).closest("form[id]").val();

        console.log('This user is now special');
        console.log(user_id);
    }
    return false;
});

And you can see that I'm also trying to get the id of the form, but when I check the console I get (an empty string). Why is that and how can I properly get the ID of the form ?

like image 641
Roland Avatar asked Mar 13 '12 13:03

Roland


People also ask

How do I find the id value of a form?

Try the following: var user_id = $(this). closest("form"). attr("id");

What is id in a form?

The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document. The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.

How do I find my Microsoft form id?

Go in and edit your form. Note the URL in the address bar of your browser. You'll see something like .../DesignPage. aspx#FormId=KFG....

How do I find my Google form id?

You should see: var FB_PUBLIC_LOAD_DATA with each element you created on their form and their respective Element ID.


1 Answers

Have you tried?

var user_id = $(this).closest("form").attr('id');

The id of the element is an attribute, you can extract any attribute (including the id) using .attr().

.val() is used to extract the value of the element, which makes no sense in a form.

More Info:

.val()
.attr()

like image 85
vdeantoni Avatar answered Sep 30 '22 19:09

vdeantoni