By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,823 Members | 730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,823 IT Pros & Developers. It's quick & easy.

Enabling/disabling a text filed from a select/option

P: n/a
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!

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Old Lady wrote:
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.

Jul 23 '05 #2

P: n/a
On Wed, 09 Feb 2005 09:52:10 GMT, Old Lady wrote:
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!
Jul 23 '05 #3

P: n/a
JRS: In article <1r***************************@40tude.net>, dated Thu,
10 Feb 2005 00:12:41, seen in news:comp.lang.javascript, al jones
<al**********@shotmail.com> posted :
On Wed, 09 Feb 2005 09:52:10 GMT, Old Lady wrote:
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!


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

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4

P: n/a
>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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.