Posted By

karlhorky on 10/27/09


Tagged

form javascript js select dropdown html default forms option parameter options parameters defaults


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

manidf


Javascript Select Dropdown Box (Defaults To Last Option Selected)


 / Published in: JavaScript
 

This javascript creates and writes out an HTML dropdown box that defaults to the last option parameter passed to page. Could be used as part of a form.

  1. // Language: Javascript within ASP (Vbscript)
  2. // Title: Javascript Select Dropdown Box (Defaults To Last Option Selected)
  3. // Description: This javascript creates and writes out an HTML <select> dropdown box that defaults to the last option parameter passed to page
  4. // Author: Karl Horky
  5. // Creation Date: 27 October 2009
  6. // Last Modified Date: 27 October 2009
  7.  
  8. var WindowLocation = window.location.href;
  9. var OptionURLString = WindowLocation.substring(WindowLocation.indexOf('option=') + 7);
  10. var CurrentOption = OptionURLString.substring(0,OptionURLString.indexOf('&')>=0?OptionURLString.indexOf('&'):OptionURLString.length);
  11. var Options = {
  12. "opt1": { "fullname": "Option 1", "selected": (CurrentOption=="opt1" ? " selected=\"selected\"":"") },
  13. "opt2": { "fullname": "Option 2", "selected": (CurrentOption=="opt2" ? " selected=\"selected\"":"") },
  14. "opt3": { "fullname": "Option 3", "selected": (CurrentOption=="opt3" ? " selected=\"selected\"":"") },
  15. "opt4": { "fullname": "Option 4", "selected": (CurrentOption=="opt4" ? " selected=\"selected\"":"") },
  16. "opt5": { "fullname": "Option 5", "selected": (CurrentOption=="opt5" ? " selected=\"selected\"":"") },
  17. "opt6": { "fullname": "Option 6", "selected": (CurrentOption=="opt6" ? " selected=\"selected\"":"") }
  18. };
  19. var OptionSelect = "<select name=\"option\">";
  20. for (Option in Options) {
  21. var OptionHash = Options[Option];
  22. OptionSelect += "<option value=\"" + Option + "\"" + OptionHash["selected"] + ">" + OptionHash["fullname"] + "</option>";
  23. }
  24. OptionSelect += "</select><br />";
  25. document.write(OptionSelect);

Report this snippet  

You need to login to post a comment.