Get the ID of a form?

.val() is used to get value of form elements, not attribute.

Use .attr():

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

You can also extract the DOMElement and use pure javascript to get the ID:

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

Try the following:

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

Will be back with an edit as soon as i test this.


Try this instead. Assuming there is a Single Form on the Page.

 var formid = $('form').attr('id');

Will help you out.


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()

Tags:

Jquery

Closest