Display the Text and Value of the Selected RadioButton using jQuery

Posted by: Suprotim Agarwal , on 10/20/2009, in Category jQuery and ASP.NET
Views: 61754
Abstract: In this short article, I will demonstrate how to Display the Text and Value of the Selected RadioButton using jQuery.
Display the Text and Value of the Selected RadioButton using jQuery
 
In this short article, I will demonstrate how to Display the Text and Value of the Selected RadioButton using jQuery. This article is a sample chapter from my EBook called 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls. The chapter content has been modified to publish it as an article. Also please note that for demonstration purposes, I have included jQuery code in the same page. Ideally, these resources should be created in separate folders for maintainability.
Let us quickly jump to the solution and see how we can display the text and value of the selected radio button:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Retrieve the Text and Value of the selected RadioButton</title>
    <link href="../CSS/Demos.css" rel="stylesheet" type="text/css" />
    <script src='../Scripts/jquery-1.3.2.min.js'
        type='text/javascript'>
    </script>   
 
    <script type="text/javascript">
        $(function() {
            var $radBtn = $("table.tbl input:radio");
            $radBtn.click(function() {
                var $radChecked = $(':radio:checked');
                $("#para").text('')
                    .append("<b>Index: </b>" +
                        $radChecked.val() + "<br/>")
                    .append("<b>Value: </b>" +
                        $radChecked.next().text());
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="smallDiv">
        <h2>Select a RadioButton to display its Text and Value</h2><br />
        The site I like the most : <br />
        <asp:RadioButtonList ID="rbl" runat="server" class="tbl"
        ToolTip="Select a Radio Button to see its text and value">   
            <asp:ListItem Text="dotnetcurry.com" Value="0"></asp:ListItem>   
            <asp:ListItem Text="devcurry.com" Value="1"></asp:ListItem>
            <asp:ListItem Text="sqlservercurry.com" Value="2"></asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <p id="para"></p>
    </div>
    </form>
</body>
</html>
Retrieving the index and value of a radio button is a very common requirement in our applications and can be easily achieved using jQuery. In this example, we first retrieve the radio buttons using
var $radBtn = $("table.tbl input:radio");
Since this is a RadioButtonList, the control gets rendered as a table. When the user clicks on any of the radio buttons, we first store the checked radiobuttons in the $radChecked variable to improve selector performance
var $radChecked = $(':radio:checked');
We then use:
$radChecked.val() to retrieve the value and $radChecked.next().text() to retrieve the text. 
Observe how we are accessing the value of a radiobutton using:
$radChecked.next().text().
This is because the RadioButton gets rendered like this:
<td>
<input id="rbl_0" type="radio" name="rbl" value="0" />
<label for="rbl_0">dotnetcurry.com</label>
</td>
To access the value, which is inside the label, we use next() which finds the very next sibling of each radiobutton, which in our case, is the label control. The value and text is displayed on the page using a paragraph (para). The output looks similar to this:
Output
See a Live Demo
This demo has been tested on IE7, IE8, Firefox 3, Chrome 2 and Safari 4.

There are plenty of such similar recipes that I have covered in my EBook over here 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls

I hope you liked this article and I thank you for viewing it.
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email  

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Shailesh on Friday, March 15, 2013 12:38 AM
Thanks a lot it help me a lot , everywhere in google given a solution to set value but no one give set Text ... you have given it Thanks a lot.
Comment posted by Vinod Kumar B C on Friday, April 5, 2013 11:47 PM
Very good one sir.. It helps me a lot. Thank you...
Comment posted by Tom on Thursday, April 18, 2013 1:13 PM
Excellent post. This is the only place I have seen $radChecked.next().text() and it is exactly what I needed. Great job.