Solved

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

Posted on 2011-03-08
3
432 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javascript form submition 2 22
Getting selected value using Jquery 3 21
[form-control] Retain values after a POST action 21 35
Problem in javascript 17 24
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

749 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