Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2011, 04:19 PM   PM User | #1
deepthiz
New Coder

 
Join Date: May 2011
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
deepthiz is an unknown quantity at this point
Javascript select box help

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
deepthiz is offline   Reply With Quote
Old 09-28-2011, 04:32 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I don't really see the point of having two separate select lists kept in sync, but here you are:-


Code:
<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?

Code:
<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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 09-28-2011 at 04:35 PM..
Philip M is offline   Reply With Quote
Old 09-28-2011, 04:56 PM   PM User | #3
deepthiz
New Coder

 
Join Date: May 2011
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
deepthiz is an unknown quantity at this point
Thanks a lot Philips!
deepthiz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 PM.


Advertisement
Log in to turn off these ads.