Solved

use onchange inside <select> to change the page display

Posted on 2006-11-11
2
213 Views
Last Modified: 2006-11-18
I need to change the page display by adding new controls depending on the selected option using onchange
how can i take the value of the selected option and send it to a method that should displays the needed controls ?
0
Comment
Question by:Sh_Rashed
2 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 250 total points
ID: 17926927
There are essentially two ways to do this: you can either display the stuff when it's selected, and hide it when it isn't, or you can create the items directly in the DOM. The first solution is probably the easiest. Here's an example of both solutions - I've tested it in firefox, but it may need tweaking in IE:

<html><head>
<script type="text/javascript">
   function doShowHide(sel) {
      // Hide everything
      for (var iX=1; iX<5; iX++) {
         document.getElementById("div" + iX).style.display = "none";
      }
      // Show the chosen item
      var id = sel.value;
      if (id > 0) {
         document.getElementById("div" + id).style.display = "block";
      }
   }
   function doAdd(sel) {
      // Find the div
      var div = document.getElementById("add1");

      // Which choice?
      var id = sel.value;
      if (id == "1") {
         var but = document.createElement("INPUT");
         but.type = "button";
         but.value ="Click Me";
         div.appendChild(but);
      } else if (id == "2") {
         var txt = document.createElement("INPUT");
         txt.value ="Any Text";
         div.appendChild(txt);
      } else if (id == "3") {
         var txt = document.createElement("INPUT");
         txt.value ="Any Text";
         div.appendChild(txt);
         var but = document.createElement("INPUT");
         but.type = "button";
         but.value ="Click Me";
         div.appendChild(but);
      }
   }
</script>
<style type="text/css">
</style>
</head><body>
Solution 1: hide &amp; show stuff depending on selection:
<select id="sel1" onchange="doShowHide(this);" >
  <option value ="0">CHOOSE AN ITEM</option>
  <option value ="1">Foo</option>
  <option value ="2">Bar</option>
  <option value ="3">Widget</option>
  <option value ="4">Thingamajig</option>
</select>
<br>
Solution 2: Change the contents of the DOM depending on selection:
<select id="sel2" onchange="doAdd(this);" >
  <option value ="0">CHOOSE AN ITEM</option>
  <option value ="1">button</option>
  <option value ="2">input</option>
  <option value ="3">input and button</option>
</select>
<div id="div1" style="background-color:red;display:none;">
   Solution 1: This displays when you make the first selection
</div>
<div id="div2" style="background-color:yellow;display:none;">
   Solution 1: This displays when you make the second selection
</div>
<div id="div3" style="background-color:blue;display:none;">
   Solution 1: This displays when you make the third selection
</div>
<div id="div4" style="background-color:green;display:none;">
   Solution 1: This displays when you make the fourth selection
</div>
<br /><br /><br />
<div id="add1" style="display:block; background-color:yellow; width:30%; height:10%;">
</div>
</body></html>
0
 

Author Comment

by:Sh_Rashed
ID: 17928699
Thank u that is so usefull..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spring dependency injection using java config 2 81
Java Exception example issues 11 84
Can't access image with tomcat/html 6 96
instanceof  operator in java 26 88
In the hope of saving someone else's sanity... About a year ago we bought a Cisco 1921 router with two ADSL/VDSL EHWIC cards to load balance local network traffic over the two broadband lines we have, but we couldn't get the routing to work consi…
There is no doubt that cloud is gaining importance. Many of you must have read about this technology and its growing importance. More and more organisations are embracing this technology not forgetting start-ups. The process begins by dipping …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

27 Experts available now in Live!

Get 1:1 Help Now