/    Sign up×
Community /Pin to ProfileBookmark

radio button to change drop down menu

Can anyone help?
Lets say you have a group of radio buttons with values of A, B, and C
I want to have a drop down box, on the same page that the values are changed by which radio button is selected
so if radio value A is selected then the drop down will have values of f1, x2, i3

if radio value B is selected then drop down will have values m1, r4, g3
and so on and so forth.

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@Ay__351_eOct 12.2007 — <i>
</i>&lt;script type="text/javascript"&gt;

function change(arr) {
var el = document.getElementById('sel');
el.options.length = 0;
for(var i = 0; i&lt; arr.length; i++ ) {
el.options[el.options.length] = new Option( arr[i], arr[i]);
}
}
&lt;/script&gt;

&lt;input type="radio" name="rad" id="r0" onclick='change(["f1", "x2", "i3"]) '&gt;&lt;label for="r0"&gt; one &lt;/label&gt;&lt;br&gt;
&lt;input type="radio" name="rad" id="r1" onclick='change(["m1", "r4", "g3"]) '&gt;&lt;label for="r1"&gt;two &lt;/label&gt;&lt;br&gt;

&lt;select id="sel"&gt;&lt;/select&gt;
Copy linkTweet thisAlerts:
@HhAaZzEeYyauthorOct 16.2007 — I ended up using a code pretty much exactly like this
[CODE]
function changer(jseries, jname, jimg, jdesc)
{
var HTML = '<span class="bgseries">' + jseries + '-Series</span><br /><span class="bgname">' + jname + '</span><br /><img src="pics/' + jimg + '"/><br /><span class="bgdetail">' + jdesc + '</span>';

document.getElementById('change').innerHTML = HTML;

var selbox = document.bucket.bwidth;
selbox.options.length = 0;
selbox.options[selbox.options.length] = new Option('Choose Width','No');
<?php
$con = mysql_connect("localhost","DATABASE","PASSWORD");
if (!$con)
connecterror();
else
{
mysql_select_db("database", $con);
$seriesa = mysql_query("SELECT DISTINCT Series FROM cbucket");
while($var = mysql_fetch_array($seriesa))
{
$seriesc = $var['Series'];
echo 'if (jseries == "'.$seriesc.'") {';
$widths = mysql_query("SELECT Width FROM cbucket WHERE Series = '$seriesc'");
while ($var2 = mysql_fetch_array($widths))
{
echo 'selbox.options[selbox.options.length] = new Option(''.$var2['Width'].'"',''.$var2['Width'].'');
';
}
echo '}
';
}
mysql_close($con);
}
?>
}
</script>


[/CODE]
Copy linkTweet thisAlerts:
@ultrasound0000Feb 19.2009 — hey HhAaZzEeYy, I like your PHP/JS solution for this.

If for some reason you still get notified when this post is updated, can you post a solution for this but without the MySQL code? I need to accomplish the same thing but do not have a MySQL back-end on my case.

Or anyone else is welcome to help
Copy linkTweet thisAlerts:
@JMRKERFeb 19.2009 — An alternate JS only solution:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Limit Selections&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="radio" name="first" value="1" onclick="choice(this)"&gt;A
&lt;input type="radio" name="first" value="2" onclick="choice(this)"&gt;B
&lt;input type="radio" name="first" value="3" onclick="choice(this)"&gt;C
&lt;br&gt;

&lt;select id='second'&gt;
&lt;/select&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/newreply.php?do=postreply&amp;t=163152

function choice(t){
var a = [1,2,3];
var b = [4,5,6,7];
var c = [8,9,10];
s = document.getElementById('second');
var sl = s.options.length;
for(var i = sl-1; i &gt;= 0 ; i--) { s.options[i] = null; }
if(t.value != 0){
var z;
switch (t.value) {
case '1' : z = a; break;
case '2' : z = b; break;
case '3' : z = c; break;
default : alert('Invalid entry'); break;
}
var l = z.length;
for(i = 0; i &lt; l; i++ ) { s.options[i] = new Option(z[i],z[i],false,false); }
}
}

&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ultrasound0000Feb 19.2009 — Hey JMRKER, thanks, your solution works really well. However, I think I realized that I did not explain myself very well on my post. What I'm trying to actually change on each radio click is the value and not the text of the select list. So the text on the selects remains the same, but I want to change the values instead.

So basically to clarify this some more, each time a if users clicks option A on the radio button, the content of the select list remains the same but each value for each item on the drop down list changes. If user clicks option B of the radio button, the names on the select list remain the same, but the values change. I need the values to change because then I use them for a computation.

Anyway to use the original code you suggested (which works like a charm) and have it change the values?

Thanks a ton!
Copy linkTweet thisAlerts:
@JMRKERFeb 19.2009 — Take out the alert for demonstration purposes:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Limit Selections&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="radio" name="first" value="1" onclick="choice(this)"&gt;1
&lt;input type="radio" name="first" value="2" onclick="choice(this)"&gt;2
&lt;input type="radio" name="first" value="3" onclick="choice(this)"&gt;3

&lt;select id='second' onchange="alert(this.value)"&gt;&lt;/select&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/newreply.php?do=postreply&amp;t=163152

function choice(t){
// format of array fields Display:Value
var a = [':','A:1','B:2','C:3'];
var b = [':','A:4','B:5','C:6'];
var c = [':','A:7','B:8','C:9'];
s = document.getElementById('second');
var sl = s.options.length;
for(var i = sl-1; i &gt;= 0 ; i--) { s.options[i] = null; }
if(t.value != 0){
var z;
switch (t.value) {
case '1' : z = a; break;
case '2' : z = b; break;
case '3' : z = c; break;
default : alert('Invalid entry'); break;
}
var l = z.length;
for(i = 0; i &lt; l; i++ ) {
tmp = z[i].split(':');
s.options[i] = new Option(tmp[0],tmp[1],false,false);
}
}
}

&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ultrasound0000Feb 19.2009 — JMRKER works amazingly, thanks so much!

One last thing, I'm trying to have the selects remember the current value selected when changed by the radio button.

So if I'm on the first select, 4th option, I want that to be remembered even when changed by the radio button. Any ideas?

Again, thanks so much for your solution
Copy linkTweet thisAlerts:
@JMRKERFeb 19.2009 — Won't work with my solution as the display is created new each time.

Might try saving selections, but that doesn't make much sense to me

as the radio button only allows one choice at a time. Therefore, only current

radio button and drop down box are valid (?) combinations of user choice.

Might try to explain what you really want to do with this. ?
Copy linkTweet thisAlerts:
@ultrasound0000Feb 19.2009 — I see ...

What I'm creating is a pricing tool. The radio buttons represent three different turnaround times. Prices change depending on turnaround time (this is why I have three different values for each select list, one set per each turnaround time).

So users will make their selections on the form and at the bottom they will be able to change the turnaround times and see the updated price on the form. So if the form does not remember the values, users will have to re-make their choices on the form. This is why I want to find a solution that will remember the choices even when different turnaround times are selected so the final price is displayed on screen instantly. I hope this makes sense...

Any thoughts?
Copy linkTweet thisAlerts:
@JMRKERFeb 20.2009 — If I understand correctly:

  • 1. The user makes initial turn-around time and price quote.

  • 2. Later, the user can change the turn-around time, which changes the price

    BUT they do not change the selection --- correct?

  • 3. If above assumption is correct, it should be fairly easy to remember

    a) the selection of the drop-down box and b) the turn-around choice.


  • You would just need to select the appropriate price using the latest turn-around choice.

    Post a sample of your turn-around and prices as it would fit into solution provided earlier.
    Copy linkTweet thisAlerts:
    @ultrasound0000Feb 20.2009 — 2. Later, the user can change the turn-around time, which changes the price

    BUT they do not change the selection --- correct?[/QUOTE]


    That's exactly correct. I'm including a simplified sample from my code below (it should run).

    [CODE]<html>
    <head>
    <script type="text/javascript">
    function choice(t) {
    //prices for one week turnaround
    var a = ['Item One:10','Item Two:20','Item Three:30'];
    //prices for three days turnaround
    var b = ['Item One:15','Item Two:25','Item Three:35'];
    // prices for same day turnaround
    var c = ['Item One:20','Item Two:30','Item Three:40'];

    s = document.getElementById('items');
    var sl = s.options.length;
    for(var i = sl-1; i >= 0 ; i--) { s.options[i] = null; }
    if(t.value != 0){
    var z;
    switch (t.value) {
    case '1' : z = a; break;
    case '2' : z = b; break;
    case '3' : z = c; break;
    default : alert('Invalid entry'); break;
    }
    var l = z.length;
    alert(z);
    for(i = 0; i < l; i++ ) {
    tmp = z[i].split(':');
    s.options[i] = new Option(tmp[0],tmp[1],false,false);
    }
    }
    }

    function computePrice(form) {

    var num_items = 0;
    var total_cost = 0;
    // set number of items
    if (document.myform.num_items.value!="" && !isNaN(document.myform.num_items.value) )
    {
    num_items = parseInt(document.myform.num_items.value);
    }
    // set item
    var items = parseInt(document.myform.items.value );
    // calculate total cost
    var total_cost = items * num_items;
    // display total cost
    document.getElementById('total_cost').innerHTML = "Total Cost is: $" + total_cost;
    }
    </script>
    </head>

    <body>
    <form id="myform" name="myform">
    <!-- my items -->
    <select id="items" onChange="computePrice(this)">
    <option value="10">Item One</option>
    <option value="20">Item Two</option>
    <option value="30">Item Three</option>
    </select><br>
    <input type="text" value="0" id="num_items" onkeyup="computePrice(this)"><br><br>
    <!-- turnaround time -->
    <input type="radio" name="turnaround" value="1" checked="checked" onClick="choice(this);computePrice(this);">Deliver in one week
    <input type="radio" name="turnaround" value="2" onClick="choice(this);computePrice(this);">Deliver in three days
    <input type="radio" name="turnaround" value="3" onClick="choice(this);computePrice();">Deliver in one day!<br><br>
    </form>
    <div id="total_cost">Total Cost: $0</div>
    </body>
    </html>[/CODE]
    Copy linkTweet thisAlerts:
    @JMRKERFeb 20.2009 — From what I can tell of your re-qualified requirements,

    you do not need to have a variable select box.

    See if this major logic change does what you want to accomplish.
    <i>
    </i>&lt;html&gt;
    &lt;head&gt;
    &lt;script type="text/javascript"&gt;
    // From: http://www.webdeveloper.com/forum/showthread.php?p=980767#post980767

    var turnaround_prices = [
    //item 1, 2, 3
    ['10','20','30'], // prices for one week turnaround
    ['15','25','35'], // prices for three days turnaround
    ['20','30','40'] // prices for same day turnaround
    ];
    function getRBtnName(GrpName) {
    var sel = document.getElementsByName(GrpName);
    var fnd = -1;
    var str = '';
    for (var i=0; i&lt;sel.length; i++) {
    if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
    }
    return fnd; // return option index of selection
    // comment out next line if option index used in line above <br/>
    // return str;
    }

    function computePrice() {
    var vndx = getRBtnName('turnaround');
    var num_items = 0;
    var total_cost = 0;
    num_items = parseInt(document.myform.num_items.value);
    if (isNaN(num_items) ) { alert('Need quantity'); return }
    selected_Item = document.myform.items.selectedIndex;
    var items = turnaround_prices[vndx][selected_Item];
    // calculate total cost
    var total_cost = items * num_items;
    // display total cost
    document.getElementById('total_cost').innerHTML = "Total Cost is: $" + total_cost;
    }
    &lt;/script&gt;
    &lt;/head&gt;

    &lt;body&gt;
    &lt;form id="myform" name="myform"&gt;
    &lt;!-- my items --&gt;
    &lt;select id="items" onchange="computePrice()"&gt;
    &lt;option value="0"&gt;Item One&lt;/option&gt;
    &lt;option value="1"&gt;Item Two&lt;/option&gt;
    &lt;option value="2"&gt;Item Three&lt;/option&gt;
    &lt;/select&gt;
    Quantity:&lt;input type="text" value="" id="num_items" size="3" onkeyup="computePrice()"&gt;&lt;br&gt;&lt;br&gt;
    &lt;!-- turnaround time --&gt;
    &lt;input type="radio" name="turnaround" value="0" onClick="computePrice();"&gt;Deliver in one week
    &lt;input type="radio" name="turnaround" value="1" onClick="computePrice();"&gt;Deliver in three days
    &lt;input type="radio" name="turnaround" value="2" onClick="computePrice();"&gt;Deliver in one day!&lt;br&gt;&lt;br&gt;
    &lt;/form&gt;
    &lt;div id="total_cost"&gt;Total Cost: $0&lt;/div&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @ultrasound0000Feb 24.2009 — JMRKER, thanks a ton for your help and insight into my problem. I used your last suggestion and it works perfectly now.
    Copy linkTweet thisAlerts:
    @JMRKERFeb 25.2009 — You're most welcome.

    Glad I was able to help.

    Good Luck!

    ?
    Copy linkTweet thisAlerts:
    @icesyDec 21.2010 — Hello,

    my general problem is change the dropdown list to radio button on Available Options .

    I found the code, but not for version 1.4.

    Can someone help me?

    Thanx for results.
    Copy linkTweet thisAlerts:
    @JMRKERDec 21.2010 — Hello,

    my general problem is change the dropdown list to radio button on Available Options .

    I found the code, but not for version 1.4.

    Can someone help me?

    Thanx for results.[/QUOTE]


    What are you talking about. ?

    If related to this thread, where?

    If not related to thread, start a new thread and explain what code you are using and where it is not working for you.
    ×

    Success!

    Help @HhAaZzEeYy spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...