My page has about 25 radio button groups. When a radio button is selected in a group, I want to perform an action Specific to that group, and so need the NAME attrib of the radio group.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Take this HTML for example :
If you wanted to deduce what group the clicked radio button belongs to you could use something like this :
Lets see whats going on here :
$("#stackExchange input:radio")– this selector will find us all of the input radio elements that are decendants of the#stackExchangeelement using the:radioselector. (Link to docs).$(this).attr('name')– here is where we extract thenameattribute of the selected radio element. (In our example – this becomessofu_group).$(this).parent()– In this case the variable$(this)refers to the radio element that was clicked – so we are selecting its parent – the#stackExchangeelement.parent().find(":radio[name='"+groupName+"']")– this line will find all of the radio buttons held within the element that have a name attribute set to ‘sofu_group’.In the example – the variable $(this) refers to the radio element that was clicked.