onChange vs. onLoad?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: onChange vs. onLoad?

  1. #1
    Join Date
    Mar 2009
    Posts
    3

    onChange vs. onLoad?

    Hi all,

    I currently have one drop-down menu that, when changed, populates another drop-down menu. It works well, but when I refresh the page, the second drop-down menu's contents disappear, while the first one's is still in place. It's kind of annoying. Is there a way to populate the second menu automatically, when the page is loaded?

    Danny

  2. #2
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    Yes, by scripting the required code immediately after adding the drop down box, or using the onchange handler. There are other, more complex handlers too.

  3. #3
    Join Date
    Mar 2009
    Posts
    3
    Thanks. What would that sort of code look like?

  4. #4
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    Something like this:
    Code:
    <select name="ddb1" id="ddb1">
    <option value="s1">Selection 1</option>
    <option value="s2">Selection 2</option>
    <option value="s3">Selection 3</option>
    </select>
    <select name="ddb2" id="ddb2">
    </select>
    <script>
    function createOption(value, text)
    { var el = document.createElement("option");
    el.setAttribute("value", value);
    el.appendChild(document.createTextNode(text));
    return el;
    }
    var select1Selected = document.getElementById("ddb1").selectedIndex;
    var selectToModify = document.getElementById("ddb2");
    switch(select1Selected)
    {
     case 0:
     case 2:
       selectToModify.appendChild(createOption("n1", "value 1"));
       selectToModify.appendChild(createOption("n2", "value 2"));
       break;
     case 1:
       selectToModify.appendChild(createOption("n3", "value 3"));
    }
    </script>
    The above example uses DOM methods to add options. You can also use JavaScript without DOM, which you can find here.

Similar Threads

  1. Replies: 1
    Last Post: 12-11-2008, 07:13 AM
  2. AJAX call at Page Onload (https)
    By AjaxNewBie in forum AJAX
    Replies: 0
    Last Post: 06-14-2007, 04:25 AM
  3. Replies: 3
    Last Post: 06-22-2006, 11:07 AM
  4. Replies: 0
    Last Post: 12-01-2000, 05:49 AM
  5. onchange event of dropdown box
    By Sean Gallagahar in forum ASP.NET
    Replies: 1
    Last Post: 11-20-2000, 07:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center