How to find parent form from element?

http://api.jquery.com/closest/ will do it. Used like this

$('#elem').closest('form');

The problem you're having is that form is a jQuery object, not a DOM object. If you want it to be the form object, you would do e.parent('form').get(0).

Furthermore, you're treating element incorrectly - jQuery takes id selectors in the form #id but you've passed it id.

Here's a working version:

function testaction(element) {
  var e = $(element);//element not element.id
  var form = e.parent('form').get(0);//.get(0) added

  alert(form.id); // undefined!!
  alert(form.action); // undefined!!
  alert(document.forms[0].action); //http://localhost/action1.html
}

See this for it in action: http://jsfiddle.net/BTmwq/

EDIT: spelling, clarity

Tags:

Jquery