I need to get a reference to the FORM parent of an INPUT when I only have a reference to that INPUT. Is this possible with JavaScript? Use jQuery if you like.
function doSomething(element) {
//element is input object
//how to get reference to form?
}
This doesn't work:
var form = $(element).parents('form:first');
alert($(form).attr("name"));
simply as:
Every input has a
form
property which points to the form the input belongs to, so simply:I needed to use element.attr('form') instead of element.form
I use firefox on Fedora 12
And one more....
I use a bit of jQuery and old style javascript - less code
This is a complete reference to the form containing the element
and in the html, you need to find that element, and add the attribut "form" to connect to that form, please refer to http://www.w3schools.com/tags/att_input_form.asp but this form attr doesn't support IE, for ie, you need to pass form id directly.