Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Listbox changes textbox to disabled and grey

Reply
Thread Tools

Listbox changes textbox to disabled and grey

 
 
BrendanMcPherson@gmail.com
Guest
Posts: n/a
 
      08-29-2006
How can I get a listbox to change a textbox on some selections.
I have a listbox which only needs to be filled in on some selections.

Is there a way I can have on some selections the textbox go
disabled and turn grey?

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      08-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> How can I get a listbox to change a textbox on some selections.
> I have a listbox which only needs to be filled in on some selections.
>
> Is there a way I can have on some selections the textbox go
> disabled and turn grey?


You can disable it, but what it looks like when disabled is up to the
browser. You may be able to affect the appearance using CSS, but that
is inconsistent across browsers.

Here's a quick example:

<script type="text/javascript">
function modTextbox()
{
document.formA.textA.disabled = !!(document.formA.selA.value%2);
}
window.onload = modTextbox;
</script>
<form name="formA" action="">
<div>
<select id="selA" onchange="modTextbox();">
<option value="0">0
<option value="1">1
<option value="2">2
<option value="3">3
</select>
<br>
<label for="textA"><input type="text" id="textA">Enable
only if selection is an even number</label>
</div>
</form>


--
Rob

 
Reply With Quote
 
 
 
 
BrendanMcPherson@gmail.com
Guest
Posts: n/a
 
      08-29-2006
Thanks Rob
I still dont get it!!!
do you have an example where the first option is normal
but second option disables textbox?



RobG wrote:
> (E-Mail Removed) wrote:
> > How can I get a listbox to change a textbox on some selections.
> > I have a listbox which only needs to be filled in on some selections.
> >
> > Is there a way I can have on some selections the textbox go
> > disabled and turn grey?

>
> You can disable it, but what it looks like when disabled is up to the
> browser. You may be able to affect the appearance using CSS, but that
> is inconsistent across browsers.
>
> Here's a quick example:
>
> <script type="text/javascript">
> function modTextbox()
> {
> document.formA.textA.disabled = !!(document.formA.selA.value%2);
> }
> window.onload = modTextbox;
> </script>
> <form name="formA" action="">
> <div>
> <select id="selA" onchange="modTextbox();">
> <option value="0">0
> <option value="1">1
> <option value="2">2
> <option value="3">3
> </select>
> <br>
> <label for="textA"><input type="text" id="textA">Enable
> only if selection is an even number</label>
> </div>
> </form>
>
>
> --
> Rob


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      08-29-2006
(E-Mail Removed) wrote:
> Thanks Rob
> I still dont get it!!!
> do you have an example where the first option is normal
> but second option disables textbox?


To disable an element that has a disabled attribute (button, input,
optgroup, option, select, textarea) set its value to true. To enable
it again, set disabled to false. The basic algorithm is:

if (condition) {
elementReference.disabled = true;
} else {
elementReference.disabled = false;
}

A shorthand method is to set the value of the disabled attribute to the
outcome of the condition statement:

elementReference.disabled = (condition);

That's effectively the same as the above 5 lines of code. The use of
"!!" in my earlier post was just to make sure that (condition) returned
true or false, it is probably not needed. Here's another example:

<form action="">
<div>
<select onchange="
if ( 1 == this.selectedIndex ) {
this.form.textA.disabled = true;
} else {
this.form.textA.disabled = false;
}
">
<option>Enable input
<option>Disable input
</select>
<input type="text" id="textA">
</div>
</form>

It's possible to get the two out of sync by reloading the page
(depending on the browser) as the input will probably become enabled
again but the selected option may not be reset. Hence my use of an
onload function to make sure they were synchronised.


--
Rob

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox disabled - cant scroll in IE - can scroll in Firefox =?Utf-8?B?Ui5OaWprYW1w?= ASP .Net 0 11-22-2006 03:46 PM
grey-out/disabled item in listbox acord Javascript 3 03-11-2006 02:08 PM
click listbox and refresh another listbox DC Gringo ASP .Net 0 04-06-2004 02:13 AM
Still trying to figure the grey level of an 18% grey card (was: If I shoot a grey card, should this end up as 127 grey?) Alan F Cross Digital Photography 40 03-02-2004 12:12 PM
If I shoot a grey card, should this end up as 127 grey? Alan F Cross Digital Photography 8 02-26-2004 09:57 AM



Advertisments