Search code examples
jqueryradio-button

How do I get values dynamically from radio buttons?


This is an on change event for anything changed on the page.

$(this).change(function(){
    type = $('input[name='+$(this).name+'radioName]:checked', '#myForm').val();
    alert(type);
});

I want to get the radio button that is clicked. So I can show the hidden input box when Profit is clicked.

<TD align=left>
    <input type=radio name="Red" value="l" checked>Loss
    <input type=radio name="Red" value="w">Win
    <input type=radio name="Red" value="p">Profit
    <INPUT value="" name="profitRed" LENGTH="5" hidden>
</TD>
<TD align=left>
    <input type=radio name="Black" value="l" checked>Loss
    <input type=radio name="Black" value="w">Win
    <input type=radio name="Black" value="p">Profit
    <INPUT value="" name="profitBlack" LENGTH="5" hidden>
</TD>

So what ever radio button gets clicked, I want to grab the name, and check the value. If the value is "p", then I want to update the corresponding text box.

If(type=="p")
{
    $("profit"+$(this).name).show();
}

I'm new to JQuery and really hope someone can help me with this. It would make things so much easier than having to check each name.

Thanks a ton


Solution

  • $('input[type="radio"]').on('click',function(){
         var value = $(this).val();
         var name = $(this).attr('name');
         if(value == 'p'){
             // add . for class or # for id
             $('#profit'+ name).show();
             // this will show the element with Id='profitBlack'
         }
    });