mpsn Posted January 6, 2012 Share Posted January 6, 2012 Hi, I want the drop down select input to change the textbox to the appropriate phone number, but it's not working: Here's the HTML: <html> <head> <script type="text/javascript" src="js_.js"></script> <body> <form id="form_3"> <select id="select_2" onclick="getPhoneNumber( this.options[this.options.selectedIndex].value );"> <option value="pizza73">Pizza 73</option> <option value="work">Work</option> <option value="school">School</option> </select> <input type="text" id="phoneNumberText" value="displays phone number" /> </form> </body> </html> Here's the external JS (js_.js): var phoneBook = new Array(); phoneBook["pizza73"] = "780 473 7373"; phoneBook["work"] = "780 444 8621"; phoneBook["school"] = "780 444 8000"; function getPhoneNumber(selected) { var showPhoneNumber = document.getElementById("phoneNumberText").value; showPhoneNumber = phoneBook[selected]; } Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/254452-help-with-select-and-text-input/ Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 So basically, you're wanting the user to be able to select a name from a list, the value of which contains a phone number? Once they select a number, you want to then output that number to the user? Have you considered using jQuery for this? It'd be extremely simple to accomplish. Quote Link to comment https://forums.phpfreaks.com/topic/254452-help-with-select-and-text-input/#findComment-1304689 Share on other sites More sharing options...
nogray Posted January 6, 2012 Share Posted January 6, 2012 change the "onclick" to "onchange" Quote Link to comment https://forums.phpfreaks.com/topic/254452-help-with-select-and-text-input/#findComment-1304713 Share on other sites More sharing options...
scootstah Posted January 6, 2012 Share Posted January 6, 2012 Working jQuery example here: http://jsfiddle.net/CGJcq/ Quote Link to comment https://forums.phpfreaks.com/topic/254452-help-with-select-and-text-input/#findComment-1304729 Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 The scootstah's example is exactly how I'd do it, although I might personally also add disabled="disabled" to the input so it can't be directly modified, but they can still copy / paste the number ( I assume that's your goal ) Quote Link to comment https://forums.phpfreaks.com/topic/254452-help-with-select-and-text-input/#findComment-1304971 Share on other sites More sharing options...
nogray Posted January 6, 2012 Share Posted January 6, 2012 Using jQuery for this simple function is way overkill. Also, disabled fields are not sent with the form when submitted. If you want to stop the user from changing the info, use readonly Quote Link to comment https://forums.phpfreaks.com/topic/254452-help-with-select-and-text-input/#findComment-1304982 Share on other sites More sharing options...
Labradoodle-360 Posted January 6, 2012 Share Posted January 6, 2012 If it's a form being submitted, you are correct. But if he's simply trying to output the number, disabling it wouldn't cause any issues, although in that case, it probably shouldn't be an input element, anyway. Quote Link to comment https://forums.phpfreaks.com/topic/254452-help-with-select-and-text-input/#findComment-1305089 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.