Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

use onchange inside <select> to change the page display

Posted on 2006-11-11
2
Medium Priority
?
230 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 750 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Aerodynamic noise is the cause of the majority of the noise produced by helicopters. The inordinate amount of noise helicopters produce is a major problem in the both a military and civilian setting. To remedy this problem the use of an aerogel coat…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

824 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