Solved

use onchange inside <select> to change the page display

Posted on 2006-11-11
2
211 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
A Short Story about the Best File Recovery Software – Acronis True Image 2017
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

13 Experts available now in Live!

Get 1:1 Help Now