Jump to content

[SOLVED] Disable text field


Nandini

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.