Monday, 8 November 2010

jQuery: Working with radio buttons

Each time I need to use radio buttons in jQuery I need to look it up - here's the best way to use them I've found so far..

<form id="my-form">
<input type="radio" name="group-name" value="value1">
<input type="radio" name="group-name" value="value2">

// set up the initial value (theValueToSet)
var $log = $("#my-form input[name=group-name]"); // #my-form is optional, but speeds up the search

// do something when the value is changed..
$log.change(function() {
var selectedValue = $(this).val();

// getting the value out again later on
var value = $("#my-form input[name=group-name]").val();

No comments:

Post a Comment