Enabling/disabling a text filed from a select/option

O

Old Lady

Hi,
I need help!
I have a SELECT/OPTION list.
My goal is to have a TEXT input field in a form that is normally
disabled, but it should become enabled when the user select one
predefined OPTION. If the user change idea and select another OPTION,
the TEXT input field should be disabled again.
I hope I'm understandable.

Sorry for my bad English.

Thank You!
 
R

RobB

Old said:
Hi,
I need help!
I have a SELECT/OPTION list.
My goal is to have a TEXT input field in a form that is normally
disabled, but it should become enabled when the user select one
predefined OPTION. If the user change idea and select another OPTION,
the TEXT input field should be disabled again.
I hope I'm understandable.

Sorry for my bad English.

Thank You!

English is fine, don't worry.

If you set a form field to disabled in your HTML, users with JavaScript
disabled will never be able to use it. It's better to disable it when
the page loads, using JS.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">

window.onload = function()
{
var s, t;
if ((s = document.getElementById('foo'))
&& (t = document.getElementById('bar'))
&& ('undefined' != typeof t.disabled))
{
t.disabled = true;
s.onchange = function()
{
var bWhich = (this.selectedIndex == 4)
t.disabled = !bWhich;
if (bWhich)
{
t.focus();
t.select();
}
}
}
s.onchange();
}

</script>
</head>
<body>
<form>
<select id="foo" name="foo" size="5">
<option value="alpha">alpha</option>
<option value="beta">beta</option>
<option value="gamma">gamma</option>
<option value="delta">delta</option>
<option value="epsilon">other</option>
</select>
<input id="bar" type="text" name="bar" value="test" />
</form>
</body>
</html>

Just set ---> this.selectedIndex == 4
to the index of the enable/disable option (JS counts from 0). You're
using the window's onload handler (same as <body onload="...">) so
you'll need to 'bundle' this with any other onload routines you're
calling.
 
A

al jones

Hi,
I need help!
I have a SELECT/OPTION list.
My goal is to have a TEXT input field in a form that is normally
disabled, but it should become enabled when the user select one
predefined OPTION. If the user change idea and select another OPTION,
the TEXT input field should be disabled again.
I hope I'm understandable.

Sorry for my bad English.

Thank You!

To aplogize for english that is better than many of for whom it's the
native tongue is .... Well, your English is quite acceptable!
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Thu,
10 Feb 2005 00:12:41, seen in al jones
To aplogize for english that is better than many of for whom it's the
native tongue is .... Well, your English is quite acceptable!

I agree with your conclusion, but consider you to be a manifestly
unqualified judge.
 
O

Old Lady

If you set a form field to disabled in your HTML, users with JavaScript
disabled will never be able to use it. It's better to disable it when
the page loads, using JS.

I tried it and it works! :)
Thank you very much for your help and sorry for this delay in
answering you, but I was out of city for a while... :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top