views:

43

answers:

2

Hello,

given following html

<input id="IBE1_IBE_NurFlug1_RadioButtonList1_0" 
       name="IBE1$IBE_NurFlug1$RadioButtonList1" value="Blue" type="radio">
<label for="IBE1_IBE_NurFlug1_RadioButtonList1_0">Blue</label>

<input id="IBE1_IBE_NurFlug1_RadioButtonList1_1" 
       name="IBE1$IBE_NurFlug1$RadioButtonList1" value="Green" checked="checked" 
       type="radio">
<label for="IBE1_IBE_NurFlug1_RadioButtonList1_1">Green</label>

http://jsfiddle.net/pCBJL/2/

how can I perform actions like

If radiobutton blue is checked do ... If radiobutton green is checked do...

thanks in advance...

A: 

Use the attribute-equals (for the name match) and :checked selectors, like this:

$(function() {
  var val = $("input[name='IBE1$IBE_NurFlug1$RadioButtonList1']:checked").val();
  if(val === "Blue") {
    //do something
  } else {
    //do something else
  }
});

You can test it here.

Since you seem to be in ASP.Net though, you'll need a selector like this in your page:

$("input[name$='RadioButtonList1']:checked").val();

This is the attribute-ends-with selector, to handle the naming container prefixes that ASP.Net appends.

Nick Craver
thanks a lot. both answers are correct. the asp.net hints are top.
+1  A: 

grab the checked item and test against its value.

$(document).ready(function() {
switch ($('input:checked').val()){
    case 'Green':
        //do something green
        break;
    case 'Blue':
        //do something blue
        break;
    default:
        //do something else
}

});

Michael