ukweb Posted May 27, 2009 Share Posted May 27, 2009 Hi I have very little experience in Javascript, but basically I have a dropdown menu with a list of manufacturers, and if the manufacturer is not listed, I want a text box to go from a disabled state to an enabled state when 'other' is selected in the dropdown. Any ideas how? I know this isnt PHP but its PHP i am familiar with, and I don;t have a clue of any forums which specialise in javascript! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 27, 2009 Share Posted May 27, 2009 <html> <head> <script> function enableOther(selObj) { //Create reference to text field textObj = document.getElementById('other_manufacturer'); //Test select field value if(selObj.options[selObj.selectedIndex].value=='OTHER') { //Enable text field textObj.disabled = false; } else { //Disable text field & clear value textObj.disabled = true; textObj.value = ''; } return; } </script> </head> <body> <form name="myform"> Manufacturer: <select name="" id="" onchange="enableOther(this);"> <option value="Manufacturer 1">Manufacturer 1</option> <option value="Manufacturer 2">Manufacturer 2</option> <option value="Manufacturer 3">Manufacturer 3</option> <option value="Manufacturer 4">Manufacturer 4</option> <option value="OTHER">Other</option> </select><br /> Other: <input type="text" name="other_manufacturer" id="other_manufacturer" disabled="disabled" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 27, 2009 Share Posted May 27, 2009 instead of <input type="text" name="other_manufacturer" id="other_manufacturer" disabled="disabled" /> <input type="text" name="other_manufacturer" id="other_manufacturer" /> <script type="text/javascript">document.getElementById( 'other_manufacturer' ).disabled = false;</script> Might be a better idea for accessibility reasons. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 27, 2009 Share Posted May 27, 2009 instead of <input type="text" name="other_manufacturer" id="other_manufacturer" disabled="disabled" /> <input type="text" name="other_manufacturer" id="other_manufacturer" /> <script type="text/javascript">document.getElementById( 'other_manufacturer' ).disabled = false;</script> Might be a better idea for accessibility reasons. Good catch, but instead of running that inline Javascript line a better approach would be to run the function provided onload of the page. Quote Link to comment 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.