mahatmanich
mahatmanich

Reputation: 11033

Radio button group validation of dynamically generated name fields

CODE:

html

<form id="myform" type="post">
    <fieldset id="myid1">
        <input id="entries_8490_burn_id_1" name="entries[8490][burn_id]" value="1" type="radio"/>
        <input id="entries_8490_burn_id_2" name="entries[8490][burn_id]" value="2" type="radio"/>
        <input id="entries_8490_burn_id_3" name="entries[8490][burn_id]" value="3" type="radio"/>
    </fieldset>
    <fieldset id="myid2">
        <input id="entries_8491_burn_id_1" name="entries[8491][burn_id]" value="1" type="radio"/>
        <input id="entries_8491_burn_id_2" name="entries[8491][burn_id]" value="2" type="radio"/>
        <input id="entries_8491_burn_id_3" name="entries[8491][burn_id]" value="3" type="radio"/>
    </fieldset>
    <input type="submit" />
</form>

JS/jquery

$( document ).ready(function() {
    $('#myform').on('submit', function(e) {
        e.preventDefault();
        $("fieldset[id^='myid']").each(function () {
           myid = this.id
           alert(myid)
           alert($(myid +" > input[name^='entries']").is(":checked"));
        });               
    });    
});

fiddle: http://jsfiddle.net/woav90mz

THE ISSUE:

When I check a radio button in one of the groups I still get "false false". Somehow the groups are not selected right! Can anyone spot my mistake?

Upvotes: 0

Views: 288

Answers (1)

lshettyl
lshettyl

Reputation: 8181

You have a missing # -> Updated fiddle

alert($("#" + myid + " > input[name^='entries']").is(":checked"));

And also, if you are just trying to ensure a radio button is checked from each group, you could simply do:

$("fieldset[id^='myid']").find(" > input[name^='entries']:checked").length;

Upvotes: 3

Related Questions