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

0

4 Answers 4

20
var selectedValue = $("input[name=form]:checked").val();
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
}
3

Alternatively, you can also try this -

    $(".modal-body input[type=radio]").each(function(){
        if (this.checked)
        {
            console.log(this.value+" is checked");
        }
    });
0
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.