38

here this is my code... here i am generating radio button dynamically using php code...

my problem is how to get which radio button selected on click of addForm() function...

<div class="modal-body">
    <div> <input type="radio" name="form" value="Form-Name">Form-Name</div>
    <div> <input type="radio" name="form" value="Kalpit-Contact">Kalpit-Contact</div>
    <div> <input type="radio" name="form" value="Kalpit-Contact test">Kalpit-Contact test</div>
</div>
<div class="modal-footer"> 
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
    <button type="button" class="btn btn-primary" onclick="addForm()">Add Page</button> 
</div>

Thanks in advance

404 Not Found
  • 1,223
  • 2
  • 22
  • 31

4 Answers4

20
var selectedValue = $("input[name=form]:checked").val();
Sarath
  • 9,030
  • 11
  • 51
  • 84
6

You should do it like this:

var radio = jQuery('input[name="form"]:checked');

You can then retrieve your radio value or other attributes:

var value = radio.val();

It's a good idea to see if any radio is checked before doing that:

if (radio != undefined)
{
    //do what you need to
}
MaGnetas
  • 4,918
  • 4
  • 32
  • 52
3

Alternatively, you can also try this -

    $(".modal-body input[type=radio]").each(function(){
        if (this.checked)
        {
            console.log(this.value+" is checked");
        }
    });
skos
  • 4,102
  • 8
  • 36
  • 59
0
var myval = $("#myForm input[type=form]:checked").val();

I would put in a div (id=myForm) and call it in the safety of the parent, just in case you have anything else on the page, like a newsletter signup or search box with options.

TGR
  • 101
  • 8