jQuery UI radio button array, default value selection

jQuery UI radio button array, default value selection

How to set a default value for a jQuery UI radio button

Consider a div with a radio button array created with the following HTML code

1
2
3
4
<div>
    <input type="radio" id="active_true" name="active[]" value="active" /><label for="active_true">Active</label> 
    <input type="radio" id="active_false" name="active[]" value="inactive" /><label for="active_false">Inactive</label>
</div>

Below you will find example jQuery code to select a default value for a radio button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$(function()
{
    //Firstly, upon document ready we call the jQuery .buttonset() function on the containing div
    //to style the jQuery UI element, like so:
    $('#activations').buttonset();
 
    //we will set a variable to test against but generally this will be a program input
    //in our case, we must test against the radio button's value being 'active' or 'inactive'
    var someValue = 'active';
 
    //next, we'll get a reference to the button array
    var btnActive = $('[name=active[]]');
    $(btnActive).each(function() 
    {
        // test the value of the radio against our variable
        if( $(this).val() == someValue )
        {
            $(this).attr('checked', true);
            $(btnActive).button("refresh");
        }
    }):
});

The most important aspect of this code is the call to the .button(“refresh”) method. Without this, you will not see any visible change to the UI radio button’s selected state.


Facebook Twitter Linkedin Digg Delicious Stumbleupon Email

2 Responses to “jQuery UI radio button array, default value selection”


  1. Dan Silver says:

    Thank you for this jquery code. I was having some trouble with it on my chrome extension, but it’s working now!

  2. Mat Garland says:

    It’s a pleasure Dan, I’m glad that the code sample has been able to assist somebody!

Leave a Reply


*


Search

Contact Us

Kromosome Industries Pty. Ltd.
A.B.N. 13007002182

Melbourne, Australia
12 / 17 Como Avenue
South Yarra, Victoria
Australia 3141
T: 0424 767 060

My status

Tweets

Posting tweet...

Latest Posts

Kromosome Smart Software Solutions

Content Management Systems

Customized CMS

There are many open source content management systems available today, enabling customization to your specific needs. We can assist you navigate your way through the maze of available systems and select the most appropriate system for your application.

read more
Kromosome Smart Software Solutions

Mobile Device Application Development

Native App vs. Web App

If you are contemplating a standards compliant web application, or a native application for mobile devices, our expertise in the area of building mobile applications can help you better understand the various platforms, their strengths and weaknesses.

read more
Kromosome Smart Software Solutions

Technobabble

Our Areas of Expertiese

  • HTML5 / XHTML
  • CSS2 & CSS3
  • Javascript / Ajax / jQuery
  • XML / JSON / JSONP
  • Flash (AS3) / Actionscript
  • PHP / Cake PHP / Symfony / Zend
  • SQL Database Development
  • CMS & E-Commerce Systems
  • Native Mobile Device Applications

read more
Kromosome Smart Software Solutions

© 2010 Kromosome Industries Pty. Ltd. A.B.N. 13 007 002 182