Solved

javascript, select one dropdown changes the selected value of another drop downlist

Posted on 2011-03-08
3
428 Views
Last Modified: 2012-05-11
I have two drop down lists.  Selecting one dropdown changes the selected value of another drop downlist.  If "default" is chosen on list1, then select "All" on list 2.  Also disable selection on list2.  If possible, use onclick.

<select name=list1>
  <option value="default">default</option>
  <option value="choice">choice</option>
</select>

<select name=list2>
  <option value="All">All</option>
  <option value="choice1">choice1</option>
  <option value="choice1">choice2</option>
</select>
0
Comment
Question by:rgbcof
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 35075868
Check this :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">

	window.onload = function() {
		//var list1 = document.getElementById("list1");
		var list1 = document.getElementsByName("list1")[0];
		updateList2(list1.options[list1.selectedIndex].value);
	}

	function updateList2(val) {
		//list2 = document.getElementById("list2");
		var list2 = document.getElementsByName("list2")[0];
		if(val == "default") {
			list2.selectedIndex = 0
			for(var i=1;i<list2.options.length;i++) {
				list2.options[i].disabled = true;
			}
		}
		else {
			for(var i=1;i<list2.options.length;i++) {
				list2.options[i].disabled = false;
			}
		}
	}
</script>
</head>
<body>
<select name="list1" onchange="updateList2(this.value);" >
  <option value="default">default</option> 
  <option value="choice">choice</option>
</select>
<select name="list2">
  <option value="All">All</option> 
  <option value="choice1">choice1</option>
  <option value="choice1">choice2</option>
</select>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:rgbcof
ID: 35080227
leakim971 you are awesome in providing so much detail and actual code.  Thank you.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35082096
You're welcome! Thanks for the points! Have a nice day!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now