a developer's notes – a semi-technical web development BLOG

April 11, 2013

How to get a radio button’s label text with jQuery

Filed under: Javascript / JQuery — Duy Nguyen @ 9:59 pm
Tags: , , , , , ,

From stackoverflow

<input type="radio" name="value" id="product1">
<label for="product1">Product One</label>

You can obtain the label’s text with this jQuery code:

var text = $("input:checked + label").text();

The + signifies the adjacent selector. It will only select a label which has a checked input next to it.

If your label element has to be placed before the input, a single CSS selector can’t get a reference to it. Using jquery, you can however get a reference to the “previous child element” with the prev() function.

var text = $("input:checked").prev().text();
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: