Solved

use onchange inside <select> to change the page display

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
dateFashion java challenge 9 153
love6 challenge java 31 133
HTTPSessionBindingListernter not configured in deployment descriptor 2 76
jdbc error in jsp application 20 91
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
We asked our MSP customer base what their favorite tools were and how they help them serve clients. We focused our questions on favorite tools in the following categories: >PSA tools >RMM tools >Alert management tools >Communication tools and Mo…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

759 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