Solved

use onchange inside <select> to change the page display

Posted on 2006-11-11
2
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Getting to know the threat landscape in which DDoS has evolved, and making the right choice to get ourselves geared up to defend against  DDoS attacks effectively. Get the necessary preparation works done and focus on Doing the First Things Right.
First of all let me say that the only language that I speak is English, but in answering questions here I often come across people whose English skills are not the best and I’d like to be able to communicate better with them, and the following descr…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

635 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