...

View Full Version : Javascript select box help



deepthiz
09-28-2011, 05:19 PM
Hi all,

I am a newbie to JS. So please extend your help suggesstion to solve this problem I have.

I have 2 select boxes. Data in one select box is corresponding to the data in the other select box. When the user comes in and selects example the 3rd element from the first or second select box the correspoinding 3rd element from the other select box should be selected.

Please help with the solution.Thanks in advance

Philip M
09-28-2011, 05:32 PM
I don't really see the point of having two separate select lists kept in sync, but here you are:-



<form name = "myform">
<select name = "sel1" onchange = "go1()">
<option value = "1">ONE</option>
<option value = "2">TWO</option>
<option value = "3">THREE</option>
</select>

<select name = "sel2" onchange = "go2()">
<option value = "1">ONE</option>
<option value = "2">TWO</option>
<option value = "3">THREE</option>
</select>
</form>

<script type = "text/javascript">
function go1() {
var d = document.myform.sel1.selectedIndex;
document.myform.sel2.selectedIndex = d;
}
function go2() {
var d = document.myform.sel2.selectedIndex;
document.myform.sel1.selectedIndex = d;
}

</script>

Or do you mean just/only when the third element is selected?


<form name = "myform">
<select name = "sel1" onchange = "go1()">
<option value = "1">ONE</option>
<option value = "2">TWO</option>
<option value = "3">THREE</option>
</select>

<select name = "sel2" onchange = "go2()">
<option value = "1">ONE</option>
<option value = "2">TWO</option>
<option value = "3">THREE</option>
</select>
</form>

<script type = "text/javascript">
function go1() {
var d = document.myform.sel1.selectedIndex;
if (d==2) {
document.myform.sel2.selectedIndex = d;
}
}
function go2() {
var d = document.myform.sel2.selectedIndex;
if (d==2) {
document.myform.sel1.selectedIndex = d;
}
}
</script>

Quizmaster: What M, named after the Hebrew word for "Institution", is the national intelligence agency of Isreal?
Contestant: Al-Qaeda.

deepthiz
09-28-2011, 05:56 PM
Thanks a lot Philips!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum