Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get form elements by name?

I would like to style all of the radio buttons with the same name in a form easily in a nice Prototype or jQuery-style shortcut.

$('billship').select('name:shipType') or something like that.

Does such a shortcut for form field names exist?

like image 264
Diodeus - James MacFarlane Avatar asked Apr 01 '09 18:04

Diodeus - James MacFarlane


People also ask

How do you find the elements of a form?

In order to access the form element, we can use the method getElementById() like this: var name_element = document. getElementById('txt_name'); The getElementById() call returns the input element object with ID 'txt_name' .

What is get element by name?

The getElementsByName() method returns a collection of elements with a specified name. The getElementsByName() method returns a live NodeList.

How do you access elements of forms using form 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.

Can be used to find all form elements in HTML document?

To get all <form> elements in the document, use the document. forms collection instead.


1 Answers

With jQuery:

$('#billship input:radio[name="shipType"]');  
$('input:radio[name="shipType"]', '#billship');

Both are equivalent, but the 2nd one performs slightly better and I personally prefer it.

With Prototype, I believe it would look like this:

$$('#billship input[type="radio"][name="shipType"]');
like image 92
Paolo Bergantino Avatar answered Sep 19 '22 05:09

Paolo Bergantino