473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Clearing Element after Changing Earlier selection

Hi all,

I know how to do this the hard way, but I suspect that there's an
easier option.

I'm creating a program that requires a series of 4 or 6 select menus.
Something like this:

<select name="1">
<option value="a">A</option>
<option value="b">B</option>
</select>

If the visitor chooses "A", they'll see:

<select name="2">
<option value="c">C</option>
<option value="d">D</option>
</select>

But if they choose "B" in that first menu, they'll see:

<select name="2">
<option value="e">E</option>
<option value="f">F</option>
</select>

Make sense? This goes on for 5 of these menus, and each menu is
dependent upon a previous selection.

My problem is when someone is already on, say, menu # 4, but then goes
back to change menu # 2. At that point, I need for menu # 3 and # 4 to
go away, as if they had never been created in the first place.

The only way I know to do is something like this:

<script>
function changeit() {
if (select2 !== "")
var select2 = "<select name='2><option value='c'>C</option><option
value='d'>D</option></select>";

document.getElementById("2").innerHTML = select2;
}
</script>

<select name="1" onChange="select2 = ''; changeit()">
<option value="a">A</option>
<option value="b">B</option>
</select>

<span id="2"></span>
But this seems to be overly complicated, especially after doing 6 of
these select menus! Is there an easier way that I'm not seeing?

TIA,

Jason

PS, all code was written specifically for this email just to help
explain my problem, so please overlook any typos.

Mar 15 '07 #1
2 1369
On Mar 15, 3:53 pm, "Jason" <jwcarl...@gmail.comwrote:
Hi all,

I know how to do this the hard way, but I suspect that there's an
easier option.

I'm creating a program that requires a series of 4 or 6 select menus.
Something like this:

<select name="1">
<option value="a">A</option>
<option value="b">B</option>
</select>

If the visitor chooses "A", they'll see:

<select name="2">
<option value="c">C</option>
<option value="d">D</option>
</select>

But if they choose "B" in that first menu, they'll see:

<select name="2">
<option value="e">E</option>
<option value="f">F</option>
</select>

Make sense? This goes on for 5 of these menus, and each menu is
dependent upon a previous selection.

My problem is when someone is already on, say, menu # 4, but then goes
back to change menu # 2. At that point, I need for menu # 3 and # 4 to
go away, as if they had never been created in the first place.

The only way I know to do is something like this:

<script>
function changeit() {
if (select2 !== "")
var select2 = "<select name='2><option value='c'>C</option><option
value='d'>D</option></select>";

document.getElementById("2").innerHTML = select2;}

</script>

<select name="1" onChange="select2 = ''; changeit()">
<option value="a">A</option>
<option value="b">B</option>
</select>

<span id="2"></span>

But this seems to be overly complicated, especially after doing 6 of
these select menus! Is there an easier way that I'm not seeing?

I would create a scheme for the select names, like:

<select name="sel_1" onchange="updateSel(this);" ...>
<option></option>
</select>
<select name="sel_2" onchange="updateSel(this);" ...>
<option></option>
</select>

and so on. Each would have a default first option. Whenever any
option is modified, the options for the next select are inserted and
those of any subsequent select are removed by setting the length of
their options property to 1.

e.g. (play code):

function updateSel( sel ){
function getNextSel (sel) {
var selNum = sel.name.split('_')[1];
return sel.form.elements['sel_' + (+selNum+1)];
}
var nextSel = getNextSel(sel);

// set the options for the next select
// based on the seleted option

// Now clear any subsequent select
while (nextSel = getNextSel(nextSel)) {
nextSel.options.length = 1;
}
}

Untested of course, but hopefully you get the idea.
--
Rob
Mar 15 '07 #2
I would create a scheme for the select names, like:
>
<select name="sel_1" onchange="updateSel(this);" ...>
<option></option>
</select>
<select name="sel_2" onchange="updateSel(this);" ...>
<option></option>
</select>

and so on. Each would have a default first option. Whenever any
option is modified, the options for the next select are inserted and
those of any subsequent select are removed by setting the length of
their options property to 1.

e.g. (play code):

function updateSel( sel ){
function getNextSel (sel) {
var selNum = sel.name.split('_')[1];
return sel.form.elements['sel_' + (+selNum+1)];
}
var nextSel = getNextSel(sel);

// set the options for the next select
// based on the seleted option

// Now clear any subsequent select
while (nextSel = getNextSel(nextSel)) {
nextSel.options.length = 1;
}

}

Untested of course, but hopefully you get the idea.

--
Rob- Hide quoted text -

Excellent idea, Rob, thanks. I follow your logic there, and I'm pretty
sure it will work perfectly!

Jason

Mar 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: kaeli | last post by:
All, I have need of a readonly select element that looks and acts disabled to the user. The problem with the disabled attribute is that the value isn't passed to the handler, so I'm using...
18
by: Niels | last post by:
Hi group, I have some problems with clearing floated divs. My usual method works fine in Konqueror, but not in Firefox. Here's an example: <html><body> <div id='left' style='float:left;...
8
by: Christopher Benson-Manica | last post by:
I'm trying to dynamically change the visibility of a table row. I can hide the row, but I'm having trouble making it visible again. This is what I have: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML...
1
by: hortoristic | last post by:
We are using JavaScript to Enable/Disable certain fields on web pages based on business rules. A simple example is if when using an option type tag, and the two options are Yes and No. If YES...
2
by: Andrey Tarasevich | last post by:
Hello Consider the following HTML code sketch <div> <img src="..." style="float: left"> <p>Paragraph text</p> </div> <hr>
2
by: S P Arif Sahari Wibowo | last post by:
Hi! Do you know how to put a form's Access-Visual-Basic-code that will force the form to be inserted, while the user has not type anything in the form, without changing focus, selection, etc.? ...
2
by: ed | last post by:
Hello- i'm having some problems getting innerhtml to clear on mozilla, but it works fine in ie. my page is setup such that i have a div: <div id="otherModel"></div> on a select from a...
13
by: andypb123 | last post by:
Hello, The onchange event fires in IE6 in a SELECT element when scrolling through the list with the up and down arrows on the keyboard. In Firefox it only fires after you hit the enter key, which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.