/    Sign up×
Community /Pin to ProfileBookmark

JQuery: How to get name and value of a radio button?

Hi,

I have a form on a webpage that asks the user 15 questions, and for each question, the user can select one of three answers from a radio group: “Yes”, “Unsure” or “No”.

The code looks something like this:

[code=html]
<p>The purpose of the program was made clear to me before I started.</p>
Yes: <input name=”1″ type=”radio” value=”Y” />
Unsure: <input name=”1″ type=”radio” value=”U” />
No: <input name=”1″ type=”radio” value=”N” />

<p>The program was user-friendly.</p>
Yes: <input name=”2″ type=”radio” value=”Y” />
Unsure: <input name=”2″ type=”radio” value=”U” />
No: <input name=”2″ type=”radio” value=”N” />

<p>All buttons and links in the program worked.</p>
Yes: <input name=”3″ type=”radio” value=”Y” />
Unsure: <input name=”3″ type=”radio” value=”U” />
No: <input name=”3″ type=”radio” value=”N” />
[/code]

.. etc etc, for 15 different questions.

What I would like to do is add a bit of JQuery that will be able to detect when a radio box is clicked, and then do something with both the [U]name[/U] and the [U]value[/U] of the selected radio box (eg, just display the name and value in an alert box, or something).

What’s the best way to do this?

Please note, I can add unique classes to each radio group if need be.

Thanks in advance.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@callumdauthorApr 01.2010 — Figured it out! w00t!

[code=html]$(document).ready(function() {
$("input:radio").change(function() {
var name1 = this.name;
var val1 = this.value;
alert(name1+val1);
});
});
[/code]
×

Success!

Help @callumd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...