?
Solved

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

Posted on 2011-03-08
3
Medium Priority
?
462 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 83

Accepted Solution

by:
leakim971 earned 1000 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 83

Expert Comment

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

830 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