Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to select all input elements of a form including those outside it?

Is there an 'official' way to get all input elements of a form including those outside of the form that use the new HTML5 'form' attribute for input elements, or do I have to make a union of some custom selectors? By official i mean standard idiom or jQuery provided. Thanks.

EDIT: See this example http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_input_form

EDIT2: I ended up doing this: $("#formId :input, :input[form='formId']")

like image 637
Paralife Avatar asked Dec 16 '11 13:12

Paralife


People also ask

How do I select all input elements with enabled?

As with other pseudo-class selector like (a:hover) is also recommended preceding it with a tag name or with some other selector, otherwise the universal selector (“*“) is implied like $( “*:enabled” ) or $( “input:enabled”). Syntax: $( "input:enabled" ).

How do you get all elements in a form?

You can get all of the elements in the form by getting the form itself, and then calling the elements property on it. var form = document. querySelector('#my-form'); var elements = form.

How can you specify that an external input element belongs to a particular form?

In HTML5, you can use the form attribute: A form-associated element is, by default, associated with its ancestor form element, but may have a form attribute specified to override this.

How do you access the elements of a form using objects?

The Form Object in HTML DOM is used to represent the HTML < form > element. This tag is used to set or get the properties of < form > element. This element can be accessed by using getElementById() method.


1 Answers

Yes, there is a way.

The form.elements collection holds all form controls - both those who are within the form, and those who are associated with it via the form attribute.

So, just get the reference to the form, and retrieve the .elements property:

var form = document.getElementById( 'form1' );
var allFormControls = form.elements;

Live demo: http://jsfiddle.net/bsFcf/

If you want to place all form controls inside a jQuery object, I recommend this:

$( $( '#form1' )[0].elements )

Live demo: http://jsfiddle.net/bsFcf/1/

like image 181
Šime Vidas Avatar answered Oct 07 '22 22:10

Šime Vidas