0

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.

1
  • Small point, but for valid XHTML your <BR>s should really be <br />s. Commented Mar 31, 2009 at 20:47

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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....
Ooo, I know what. You have to wrap your code around document.ready
|
0

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)');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.