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']")
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" ).
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With