Nandini Posted April 1, 2009 Share Posted April 1, 2009 Hi I want to disable html text field by selecting on select box. i have a select box with two options as Enable and Disable. If i select Disable the text field should be disable. if i select enable the text field should be enable. These operations should be done without page refresh by using javascript. I have implemented some code. But its not working in I.E. But in other browsers its working fine. Please check my code and let me know mistakes. here is my script. <html> <head> <script type="text/javascript"> function disable() { document.getElementById("mySelect").disabled=true; } function enable() { document.getElementById("mySelect").disabled=false; } </script> </head> <body> <form> <select name="s"> <option onclick="enable()" value="Enable">Enable</option> <option onclick="disable()" value="Disable">Disable</option> </select> <br /><br /> <input type="text" id="mySelect" name="t"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted April 1, 2009 Share Posted April 1, 2009 try readOnly instead of disabled. You might have to have "cross browser" solution. Quote Link to comment Share on other sites More sharing options...
ranjuvs Posted April 1, 2009 Share Posted April 1, 2009 Hi Nandini, Try this <html> <head> <script type="text/javascript"> function enable() { var obj = document.getElementById('s'); var txtObj = document.getElementById('mySelect'); var selValue = obj.options[obj.selectedIndex].value if(selValue === "Enable"){ txtObj.disabled=false; txtObj.style.backgroundColor = ""; }else if(selValue === "Disable"){ txtObj.disabled=true; txtObj.style.backgroundColor = "#cccccc"; } } </script> </head> <body> <form> <select id="s" name="s" onchange="enable()"> <option value="Enable">Enable</option> <option value="Disable">Disable</option> </select> <br /><br /> <input type="text" id="mySelect" name="mySelect" /> </form> </body> </html> I have also added code to change the background color. this is because in I.E disabled and enabled textbox will look same. Regards Ranju Quote Link to comment Share on other sites More sharing options...
Nandini Posted April 1, 2009 Author Share Posted April 1, 2009 Thanq ranjuvs your code was working fine. i have also got some code. Seet this. <script type="text/javascript"> function dis() { switch(document.getElementById('enadis').value) { case "enable": document.getElementById("mySelect").disabled=false; break; case "disable": document.getElementById("mySelect").disabled=true; break; } } </script> <form> <select id="enadis" onchange="dis();" name="s"> <option value="enable">Enable</option> <option value="disable">Disable</option> </select> <br /><br /> <input type="text" id="mySelect" name="mySelect"> </form> Anyhow your code was workin fine. Thanq very much Quote Link to comment Share on other sites More sharing options...
Nandini Posted April 1, 2009 Author Share Posted April 1, 2009 Hi Sorry for the disturbance. i have two select box named as call-monitor and call-barge. Both select box contain options as Enable and Disable. Below i have one text field. If user select both select box as Disable then only the text field should be disable with out pagerefresh. how can i do this. please help me, this is urgent. Quote Link to comment Share on other sites More sharing options...
ranjuvs Posted April 1, 2009 Share Posted April 1, 2009 Try this <html> <head> <script type="text/javascript"> function enable() { var obj1 = document.getElementById('call-monitor'); var obj2 = document.getElementById('call-barg'); var txtObj = document.getElementById('mySelect'); var selValue1 = obj1.options[obj1.selectedIndex].value; var selValue2 = obj2.options[obj2.selectedIndex].value; if(selValue1 === "Enable" || selValue2 === "Enable"){ txtObj.disabled=false; txtObj.style.backgroundColor = ""; }else if(selValue1 === "Disable" && selValue2 === "Disable"){ txtObj.disabled=true; txtObj.style.backgroundColor = "#cccccc"; } } </script> </head> <body> <form> <select id="call-monitor" name="call-monitor" onChange="enable()"> <option value="Enable">Enable</option> <option value="Disable">Disable</option> </select> <br /><br /> <select id="call-barg" name="call-barg" onChange="enable()"> <option value="Enable">Enable</option> <option value="Disable">Disable</option> </select> <br /><br /> <input type="text" id="mySelect" name="mySelect" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 You would probably of gotten a better response by putting this in the proper forum (Javascript Help). For future reference I would check where you are posting stuff as you will get better information from that forum and better help, as they know Javascript. Especially since you state it is "urgent". Just because you need it done as soon as you can get it done, does not mean you should just post it where ever you please. Quote Link to comment Share on other sites More sharing options...
Nandini Posted April 1, 2009 Author Share Posted April 1, 2009 Thanq my dear friend. Its working fine now. Now onwards i will post the topics related to particular categories. Thanq thanq very much. 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.