Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery find form element by attributes name and value?

Tags:

jquery

forms

I am trying to check a radio button but all I have as a ref to the element is the name attribute and the elements value.

I need some way to check the correct radio where name="accounts\['+i+'\]\[account_status\]\[selected\]" and value="current" in "#form1".

I have the code below but it checks the last radio button.

$('#form1 input[name="accounts\\['+i+'\\]\\[account_status\\]\\[selected\\]"]').attr('checked', true);

Thanks

like image 317
jonny Avatar asked May 27 '11 01:05

jonny


People also ask

How to select an element by its name jQuery?

Answer: Use the Attribute Selector You can use the CSS attribute selectors to select an HTML element by name using jQuery. The attribute selectors provide a very flexible and powerful mechanism for selecting elements.

How do you find the value of an element with a name instead of ID?

Just type the name of the element without "<" and ">" characters. For example type P, not <P> if the answer is the <P> element.

What jQuery syntax selects HTML elements by a specific attribute and its value?

The [attribute|=value] selector selects each element with a specified attribute, with a value equal to a specified string (like "en") or starting with that string followed by a hyphen (like "en-us").

How get data attribute value in jQuery?

You can use this jquery attr() syntax for get data-id attribute value. $("selector"). data("data-textval"); You can use this jquery attr() syntax for get data-textval attribute value.


1 Answers

You seem to have left out the value part. Because of that, it's trying to check all of them regardless of value. Since only one radiobutton in a group can be checked at a time, only the last is checked in the end.

Add the value on the end.

input[name='something'][value='something']

http://jsfiddle.net/KsJCP/

like image 64
James Montagne Avatar answered Oct 02 '22 13:10

James Montagne