By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

Dropdown Menu- Choose 'Other' selection to show hidden textbox

P: 67
good evening,

I am trying to allow my users to enter in text if they don't find their option on my drop down menu. In the code below, I can get a text box to show up when I select 'Other' on the drop down menu. However, I cannot get it to work when I try to select 'Other' on the second drop down menu. Any ideas about how I can start getting the second drop down menu to show a text box just like the first one does?

Also, how do I get the text box to appear on the same line the dropdown menu was? By using this code, my formatting is kind of unprofessional.

Thank you for any insight......Jerry




Expand|Select|Wrap|Line Numbers
  1. <p>Company:     <select name="company" id="mySelect" onChange="swap();" style=display:inline;">
  2. <option selected="x"></option>
  3. <option value="y">y</option>
  4. <option value="z">z</option>
  5. <option value="Other">Other..</option>
  6. </select>
  7. <input type="text" name="company" id="myText" style="display:none;" onclick="swapback()"></p>
  8. <script type="text/javascript">
  9. function swap() {
  10. if(document.getElementById('mySelect').value == 'Other'){
  11. document.getElementById('mySelect').style.display = 'none';
  12. document.getElementById('myText').style.display = 'block';
  13. document.getElementById('myText').focus();
  14. }
  15. }
  16. function swapback() {
  17. document.getElementById('mySelect').selectedIndex = 0;
  18. document.getElementById('mySelect').style.display = 'block';
  19. document.getElementById('myText').style.display = 'none';
  20. }
  21. </script>
  22. <p>Company:     <select name="company" id="mySelect" onChange="swap();" style=display:inline;">
  23. <option selected="x"></option>
  24. <option value="y">y</option>
  25. <option value="z">z</option>
  26. <option value="Other">Other..</option>
  27. </select>
  28. <input type="text" name="company" id="myText" style="display:none;" onclick="swapback()"></p>
  29. <script type="text/javascript">
  30. function swap() {
  31. if(document.getElementById('mySelect').value == 'Other'){
  32. document.getElementById('mySelect').style.display = 'none';
  33. document.getElementById('myText').style.display = 'block';
  34. document.getElementById('myText').focus();
  35. }
  36. }
  37. function swapback() {
  38. document.getElementById('mySelect').selectedIndex = 0;
  39. document.getElementById('mySelect').style.display = 'block';
  40. document.getElementById('myText').style.display = 'none';
  41. }
  42. </script>
Nov 8 '08 #1
Share this Question
Share on Google+
3 Replies


jhardman
Expert 2.5K+
P: 3,405
Jerry,

I appreciate that you trust the answers that you get in the ASP forum :-) , but this question has nothing to do with ASP. In the future, please notice in which forum you are posting.

The problem is that both selects and both text boxes have the same ID - and they really need to have their own distinct ID, when the javascript function is called, it has no way to know which select and which textbox you want are writing about. I believe the function searches through the document and uses the first element with that ID. Anyway, I would have to think a while about how to best do this, but I would first pass the element that calls the function into the function call so the function knows which select called it- like this:
Expand|Select|Wrap|Line Numbers
  1. <select name="company" id="mySelect" 
  2. onChange="swap(this);" style=display:inline;">
  3. <!--
  4. ...
  5. -->
  6. function swap(caller) {
  7. if(caller.value == 'Other'){
  8. //something here
  9. }
  10.  
Then maybe one of the javascript experts can tell you what to do from there.

Jared
Nov 9 '08 #2

P: 67
thank you for your help and i apologize for putting this in the asp section originally.

I decided to just add an additional text box for the user to input the "other" section instead of messing around with the hidden box.

I plan to revisit this section after my site is up and running to make it better but for now, the extra text box will do.

thanks again.

Jerry
Nov 9 '08 #3

acoder
Expert Mod 15k+
P: 16,027
What jhardman said was correct and should have put you in the right direction.

Two more quick points:
1. You've forgotten the opening quote in the style attribute.
2. There's no need to include the JavaScript code twice - in fact, you should avoid it. Write it once and make it generic to reuse it for the second select.
Nov 10 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.