Connecting Tech Pros Worldwide Help | Site Map

Check box and text box disable

Jim
Guest
 
Posts: n/a
#1: Jul 23 '05
I have a text box and a check box, by default the page should load and
the text box be disabled..I want it so that when you click the check
box the text box diabled = false...but, when you uncheck the check box
I want the disabled to = true again..heres what I have so far

<input type="checkbox" name="chxrequest"
onclick="javascript:enableField()" value="ON" style="float: "left" >
<script language="javascript">

function enableField()
{
if (chxrequest.checked)= true
{
document.frmcallreport.txtrequest.disabled= false;
}
if (chxrequest.checked)= false
{
document.frmcallreport.txtrequest.disabled= true;

}

} </script>

<input type = "text" id="txtrequest" name="txtrequest" size="20"
disabled></td>
Lee
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Check box and text box disable


Jim said:[color=blue]
>
>I have a text box and a check box, by default the page should load and
>the text box be disabled..I want it so that when you click the check
>box the text box diabled = false...but, when you uncheck the check box
>I want the disabled to = true again[/color]

You want the disabled attribute of txtrequest to have the
opposite boolean value of the checked attribute of chxrequest:

<input type="checkbox"
name="chxrequest"
onclick="this.form.txtrequest.disabled=!this.check ed"
value="ON"
style="float:left">

<input type="text"
id="txtrequest"
name="txtrequest"
size="20"
disabled>

Dr John Stockton
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Check box and text box disable


JRS: In article <729757f9.0406031227.10a67aff@posting.google.com >, seen
in news:comp.lang.javascript, Jim <jim.ferris@motorola.com> posted at
Thu, 3 Jun 2004 13:27:04 :
[color=blue]
>function enableField()
>{
>if (chxrequest.checked)= true
>{
>document.frmcallreport.txtrequest.disabled= false;
>}
> if (chxrequest.checked)= false
> {
> document.frmcallreport.txtrequest.disabled= true;
>
> }
>
> }[/color]


(a) In javascript, = is assignment, not comparison
(b) It is almost never desirable to compare with true or false
(c) For the body of the function, this is simpler :
document.frmcallreport.txtrequest.disabled = ! chxrequest.checked
(d) The function would be more general / more expressive with chxrequest
as a parameter
(e) ISTM that you need a better instructor, or a more competently-
written book; for the latter, see the newsgroup FAQ.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Randy Webb
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Check box and text box disable


Dr John Stockton wrote:
[color=blue]
> JRS: In article <729757f9.0406031227.10a67aff@posting.google.com >, seen
> in news:comp.lang.javascript, Jim <jim.ferris@motorola.com> posted at
> Thu, 3 Jun 2004 13:27:04 :
>
>[color=green]
>>function enableField()
>>{
>>if (chxrequest.checked)= true
>>{
>>document.frmcallreport.txtrequest.disabled= false;
>>}
>>if (chxrequest.checked)= false
>>{
>>document.frmcallreport.txtrequest.disabled= true;
>>
>>}
>>
>> }[/color]
>
>
>
> (a) In javascript, = is assignment, not comparison[/color]

That depends, directly on the browser and how the script tag is
constructed. In Netscape 4.xx with language="javascript1.2", then the =
does indeed do a comparison.
[color=blue]
> (b) It is almost never desirable to compare with true or false
> (c) For the body of the function, this is simpler :
> document.frmcallreport.txtrequest.disabled = ! chxrequest.checked[/color]

But this is better:

document.frmcallreport.txtrequest.disabled =
!document.frmcallreport.chxrequest.checked

the chxrequest.checked shortcut is an IE-ism.[color=blue]
> (d) The function would be more general / more expressive with chxrequest
> as a parameter
> (e) ISTM that you need a better instructor, or a more competently-
> written book; for the latter, see the newsgroup FAQ.[/color]

No comment.



--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Closed Thread