Wednesday, October 2, 2013

Add radio input dynamically to jQuery Mobile fieldset controlgroup

I’ve seen variations on this question yet not my specific case on SO, so here goes.

I’m having trouble adding a new radio input dynamically to an existing control group using jQuery Mobile. It will add, yet the style is not correct & it doesn’t appear to interact with the other inputs correctly.

Here’s my fiddle:
http://jsfiddle.net/cjindustries/3mQF6/

Can anyone see what I’m doing wrong? Or is is a case of regenerating the whole fieldset /controlgroup again? 🙁

Thanks! Code below too…

        <div id="testPage" data-role="page">        <div data-role="content">    <fieldset data-role="controlgroup">        <legend>Radio buttons, vertical controlgroup:</legend>            <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked">            <label for="radio-choice-1">Cat</label>            <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2">            <label for="radio-choice-2">Dog</label>            <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3">            <label for="radio-choice-3">Hamster</label>            <input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4">            <label for="radio-choice-4">Lizard</label>    </fieldset>        </div>    </div>$('<input type="radio" name="radio-choice-5" id="radio-choice-5"><label for="radio-choice-5">Horse</label>')    .appendTo("fieldset");$("fieldset").trigger('create');

You need to use name="radio-choice-1" as you have used in above. This solves you behavior problem

Behavior problem solved with below change
Your modified code for dynamically adding radio.

 $('<input type="radio" name="radio-choice-1" id="radio-choice-5"><label for="radio-choice-5">Horse</label>').appendTo("fieldset");

Style problem solved with below change
Just recreate div instead of fieldset

 $("div").trigger('create');

Check Fiddle Demo

No comments:

Post a Comment