/    Sign up×
Community /Pin to ProfileBookmark

Adding <option> Tag…

Hello.

I’m having an issue adding new options based on certain selections. Here’s the code.

[CODE]
<html><head><title>…</title>
<script type=”text/javascript”>

function checkSelected (the_choice){

var target_tag = document.getElementsByTagName(“select”)[1];
var new_option1 = document.createElement(“option”);
var new_option2 = document.createElement(“option”);
var first_text = document.createTextNode(“Westside”);
var sec_text = document.createTextNode(“Southside”);

//Isaiah’s Group

var new_option3 = document.createElement(“option”);
var new_option4 = document.createElement(“option”);
var thd_text = document.createTextNode(“Suburbs”);
var frth_text = document.createTextNode(“Southside”);

//Keith’s Group

new_option1.appendChild(first_text);
new_option2.appendChild(sec_text);

//Isaiah’s Options

new_option3.appendChild(thd_text);
new_option4.appendChild(frth_text);

if (the_choice==”Isaiah”){

removePreviousOpts();

target_tag.appendChild(new_option1);
target_tag.appendChild(new_option2);
}

else if (the_choice==”Keith”){

removePreviousOpts();

target_tag.appendChild(new_option3);
target_tag.appendChild(new_option4);
}

else if (the_choice==”Please select”)
{
removePreviousOpts();
}

}

function removePreviousOpts(){

var the_select = document.getElementsByTagName(“select”)[1];
var the_options = the_select.getElementsByTagName(“options”);

for (var i=0; i<the_options.length; i++){

the_options[i].parentNode.removeChild(the_options[i]);

}
}

</script>

<style type=text/css>

#the_table {position:absolute; left:200px; top:200px;}

</style>
</head>
<body>
<div id=”the_table”>
<table>
<tr>
<td>
<select onChange=”checkSelected(this.option.value)”;>
<option>Please Select</option>
<option>Isaiah</option>
<option>Keith</option>
</select>
</td>
<td>
<select></select>
</td>
</tr>
</table>
</div>
</body>
</html>
[/CODE]

Can’t really see where I’m wrong at…

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@FangJan 24.2009 — Incorrect referencing and IE requires the attribute [I]value[/I]: &lt;select onchange="checkSelected(this.value);"&gt;
&lt;option value=""&gt;Please Select&lt;/option&gt;
&lt;option value="Isaiah"&gt;Isaiah&lt;/option&gt;
&lt;option value="Keith"&gt;Keith&lt;/option&gt;
&lt;/select&gt;
Copy linkTweet thisAlerts:
@banksworld1986authorJan 24.2009 — Okay Fang,

thanks...

I've made those and more changes and it's close to working. Now it's not showing the new options, when I alternate from name to name.. I'm sure it has something to do with how I'm using "removePreviousOpts()"...any suggestions..

Update Code:

[CODE]
<html><head><title>...</title>
<script type="text/javascript">

function checkSelected (the_choice){

var target_tag = document.getElementsByTagName("select")[1];
var new_option1 = document.createElement("option");
var new_option2 = document.createElement("option");
var first_text = document.createTextNode("Westside");
var sec_text = document.createTextNode("Southside");

//Isaiah's Group

var new_option3 = document.createElement("option");
var new_option4 = document.createElement("option");
var thd_text = document.createTextNode("Suburbs");
var frth_text = document.createTextNode("Southside");

//Keith's Group

new_option1.appendChild(first_text);
new_option2.appendChild(sec_text);

//Isaiah's Options

new_option3.appendChild(thd_text);
new_option4.appendChild(frth_text);


if (the_choice=="Isaiah"){

removePreviousOpts();
target_tag.appendChild(new_option1);
target_tag.appendChild(new_option2);
}

else if (the_choice=="Keith"){

removePreviousOpts();
target_tag.appendChild(new_option3);
target_tag.appendChild(new_option4);
}


}


function removePreviousOpts(){

var the_select = document.getElementsByTagName("select")[1];
var the_options = the_select.childNodes;

for (var i=1; i<the_options.length; i--){

the_options[i].parentNode.removeChild(the_options[i]);

}
}

</script>

<style type=text/css>

#the_table {position:absolute; left:200px; top:200px;}

</style>
</head>
<body>
<div id="the_table">
<table>
<tr>
<td>
<select onchange="checkSelected(this.value);">
<option value="">Please Select</option>
<option value="Isaiah">Isaiah</option>
<option value="Keith">Keith</option>
</select>
</td>
<td>
<select></select>
</td>
</tr>
</table>
</div>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@FangJan 24.2009 — function removePreviousOpts(){
var the_select = document.getElementsByTagName("select")[1];
the_select.options.length=null;
}
Copy linkTweet thisAlerts:
@banksworld1986authorJan 24.2009 — function removePreviousOpts(){
var the_select = document.getElementsByTagName("select")[1];
the_select.options.length=null;
}
[/QUOTE]


Okay Fang,

just an educational question :o... so why wouldn't my removePreviousOpts() code be a good solution? I thought it successfully removes the options by looking at the last one in the length first and then decrementing to get the initial..

so I thought that I was just calling the function incorrectly...?..
Copy linkTweet thisAlerts:
@FangJan 25.2009 — The logic of removing them in reverse order was correct, but the statement is wrong:function removePreviousOpts(){
var the_select = document.getElementsByTagName("select")[1];
var the_options = the_select.childNodes;
for (var i=the_options.length-1; i&gt;=0; i--){
the_options[i].parentNode.removeChild(the_options[i]);
}
}
It would work like this.

Setting the options length to null is more concise.
Copy linkTweet thisAlerts:
@wileyscouserJan 25.2009 — Hi,

do you have the html & css files so I can see exactly how this looks and works please?

Thanks,

Wiley
Copy linkTweet thisAlerts:
@FangJan 25.2009 — All the code has been included by the OP.
Copy linkTweet thisAlerts:
@wileyscouserJan 25.2009 — Thanks Fang,

Sorry but what do you mean by "OP". See, I've got so much to learn.

What I'm not clear on is where the code goes in the html file.

I've taken the files I posted a step further if you'd like to look at them.

My question is obviously about the first 2 selection lists. When I select a category in the first, the options in the second need to change to reflect the first selection.

So the second sub list would be Red Van, Yellow Van and so on.

Files attached.

Thanks,

Wiley
Copy linkTweet thisAlerts:
@FangJan 25.2009 — OP Original Poster

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" /&gt;
&lt;title&gt;Drop down list&lt;/title&gt;
&lt;meta name="Description" content="Drop down list" /&gt;
&lt;meta name="Keywords" content="lost in space" /&gt;
&lt;meta name="Author" content=""/&gt;
&lt;meta name="robots" content="index,follow"/&gt;
&lt;link href="dropdown.css" rel="stylesheet" type="text/css" /&gt;
&lt;script type="text/javascript" src="options.js"&gt;&lt;/script&gt;
&lt;body&gt;
&lt;div id= "main"&gt;
&lt;div id = "quoterequest"&gt;
&lt;form method="post" action="fieldset.html"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Vehicle Details&lt;/legend&gt;
&lt;p&gt;&lt;label for="category"&gt;Select Vehicle Category&lt;/label&gt;
&lt;select name="category" id="category"&gt;
&lt;option value="Truck"&gt;Truck&lt;/option&gt;
&lt;option value="Van"&gt;Van&lt;/option&gt;
&lt;option value="Car"&gt;Car&lt;/option&gt;
&lt;/select&gt;
&lt;/p&gt;
&lt;p&gt;&lt;label for="vehicletype"&gt;Select Vehicle Type&lt;/label&gt;
&lt;select name="vehicletype" id="vehicletype"&gt;
&lt;option value="blue"&gt;Blue truck&lt;/option&gt;
&lt;option value="red"&gt;Red truck&lt;/option&gt;
&lt;option value="green"&gt;Green truck&lt;/option&gt;
&lt;option value="yellow"&gt;Yellow truck&lt;/option&gt;
&lt;/select&gt;
&lt;/p&gt;
&lt;p&gt;&lt;label for="weeks"&gt;Hire Period (weeks):&lt;/label&gt;
&lt;select name="weeks" id="weeks"&gt;
&lt;option value="daysonly"&gt;0 weeks - days only&lt;/option&gt;
&lt;option value="1week"&gt;1 week&lt;/option&gt;
&lt;option value="2weeks"&gt;2 weeks&lt;/option&gt;
&lt;option value="3weeks"&gt;3 weeks&lt;/option&gt;
&lt;option value="4weeks"&gt;4 weeks&lt;/option&gt;
&lt;option value="morethan4weeks"&gt;More than 4 weeks&lt;/option&gt;
&lt;/select&gt;
&lt;/p&gt;
&lt;p&gt;&lt;label for="hireperioddays"&gt;Hire Period (days):&lt;/label&gt;
&lt;select name="hireperioddays" id="hireperioddays"&gt;
&lt;option value="4hours"&gt;4 hours&lt;/option&gt;
&lt;option value="1day"&gt;1 day&lt;/option&gt;
&lt;option value="1day"&gt;2 days&lt;/option&gt;
&lt;option value="1day"&gt;3 days&lt;/option&gt;
&lt;option value="1day"&gt;4 days&lt;/option&gt;
&lt;option value="1day"&gt;5 days&lt;/option&gt;
&lt;option value="1day"&gt;6 days&lt;/option&gt;
&lt;/select&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for="startdate"&gt;Start Date:&lt;/label&gt;
&lt;input type="text" name="startdate" id="startdate" class="txt" /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for="enddate"&gt;End Date:&lt;/label&gt;
&lt;input type="text" name="enddate" id="enddate" class="txt" /&gt;
&lt;/p&gt;
&lt;label for="insurance"&gt;Insurance Required?:&lt;/label&gt;
&lt;input type="checkbox" name="insurance" id="insurance" value="insurance" /&gt;
&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;&lt;legend&gt;Personal Details&lt;/legend&gt;&lt;/p&gt;
&lt;p&gt;
&lt;label for="name"&gt;Name:&lt;/label&gt;
&lt;input type="text" name="name" id="name" class="txt" /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for="companyname"&gt;Company Name:&lt;/label&gt;
&lt;input type="text" name="companyname" id="companyname" class="txt" /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for="telephone"&gt;Telephone Number:&lt;/label&gt;
&lt;input type="text" name="telephone" id="telephone" class="txt" /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for="fax"&gt;Fax Number:&lt;/label&gt;
&lt;input type="text" name="fax" id="fax" class="txt" /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for="email"&gt;Email:&lt;/label&gt;
&lt;input type="text" name="email" id="email" class="txt" /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for="comments"&gt;Comments:&lt;/label&gt;
&lt;textarea name="comments" id="comments" cols="30" rows="4"&gt;&lt;/textarea&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;/fieldset&gt;
&lt;p&gt;
&lt;label for="submit"&gt;&lt;/label&gt;
&lt;input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn" /&gt;
&lt;/p&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

options.jswindow.onload=function() {
document.getElementById('category').onchange=function() {removeOptions('vehicletype');addOptions(this.value, 'vehicletype');};
// store default html values (1st option)
var opt=document.getElementById('vehicletype').options;
var first=document.getElementById('category').options[0].value;
vehicles[first]=[];
for(var i=0; i&lt;opt.length-1; i++) {
vehicles[first][i]=opt[i].text;
}
}

var vehicles=[]; // add more vehicles and vehicle types as required
vehicles['Van']=['E-150', 'E-250', 'E-350'];
vehicles['Car']=['sedan', 'convertable', 'coupe'];

function removeOptions(objID){
var the_select=document.getElementById(objID);
the_select.options.length=null;
}

function addOptions(choice, objID){
for(var i=0; i&lt;vehicles[choice].length; i++) {
var opt=document.createElement("option");
var text=vehicles[choice][i];
var tNode=document.createTextNode(text);
opt.appendChild(tNode);
opt.setAttribute('value', text);
document.getElementById(objID).appendChild(opt);
}
}
Copy linkTweet thisAlerts:
@banksworld1986authorJan 25.2009 — The logic of removing them in reverse order was correct, but the statement is wrong:function removePreviousOpts(){
var the_select = document.getElementsByTagName("select")[1];
var the_options = the_select.childNodes;
for (var i=the_options.length-1; i&gt;=0; i--){
the_options[i].parentNode.removeChild(the_options[i]);
}
}
It would work like this.

Setting the options length to null is more concise.[/QUOTE]


Thanks Fang for responding...

really appreciate the advice...?
×

Success!

Help @banksworld1986 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.27,
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,
)...