views:

507

answers:

2

I have a project where I need to give the users several different sets of radio button options based on a given value they select in a drop down menu.

For instance..

<select id="aaa">
<option>red</option>
<option>blue</option>
<option>other</option>
</select>

<div id="abc">
Input<BR>
option 1 <input type="radio" name="colorinput" value="1" />
option 2 <input type="radio" name="colorinput" value="2"  />
</div>
<BR>
<div id="def">
Description<BR>
option 1 <input type="radio" name="colordesc" value="1" />
option 2 <input type="radio" name="colordesc" value="2" />
</div>
<BR>

I would simply like to add/remove options from either(or both) lists of radio options each time they make a different selection.

A: 

You can just do this:

$(document).ready(function() {
    // add a new input when a new color is chosen, for example
    $('#aaa').change(function() {
        var radio = $('<input>').attr({
            type: 'radio', name: 'colorinput', value: '3'
        });
        $(':radio:last-child', '#abc').after(radio).after('option 3 ');
    });
});

It is dynamically creating a new input and inserting it after the last radio inside the #abc element.

Paolo Bergantino
I think this is exactly what I'm looking for. I can't seem to get it to work though. No errors or anything.. just doesn't add the radio option.
Did you get my latest revision? I tested it on a page with the HTML you have and it works.
Paolo Bergantino
yes I got it.. but for some reason it doesn't seem to want to work. Any ideas to what it could be on my end?
No idea. Nothing happens when you change the select? I'll upload a working version in a second....
Paolo Bergantino
Ooo, I know what. You have to wrap your code around document.ready
Paolo Bergantino
I updated my code to show this.
Paolo Bergantino
yay that worked! thanks Paolo!
The radiobutton created like this cannot be selected in IE6.Read this post:http://stackoverflow.com/questions/1061526/dynamically-generated-radiobutton-by-jquery-cannot-be-selected-in-ie6
Billy
A: 

From Paolo Bergantino's post try instead of:

$(':radio:last-child', '#abc').after(radio).after('option 3 ');

This:

$(':radio:last-child', '#abc').after(radio).after('option:eq(2)');

Lathan