Check box and text box disable

Discussion in 'Javascript' started by Jim, Jun 3, 2004.

  1. Jim

    Jim Guest

    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>
    Jim, Jun 3, 2004
    #1
    1. Advertising

  2. Jim

    Lee Guest

    Jim said:
    >
    >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


    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.checked"
    value="ON"
    style="float:left">

    <input type="text"
    id="txtrequest"
    name="txtrequest"
    size="20"
    disabled>
    Lee, Jun 3, 2004
    #2
    1. Advertising

  3. JRS: In article <>, seen
    in news:comp.lang.javascript, Jim <> posted at
    Thu, 3 Jun 2004 13:27:04 :

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



    (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.
    Dr John Stockton, Jun 4, 2004
    #3
  4. Jim

    Randy Webb Guest

    Dr John Stockton wrote:

    > JRS: In article <>, seen
    > in news:comp.lang.javascript, Jim <> posted at
    > Thu, 3 Jun 2004 13:27:04 :
    >
    >
    >>function enableField()
    >>{
    >>if (chxrequest.checked)= true
    >>{
    >>document.frmcallreport.txtrequest.disabled= false;
    >>}
    >>if (chxrequest.checked)= false
    >>{
    >>document.frmcallreport.txtrequest.disabled= true;
    >>
    >>}
    >>
    >> }

    >
    >
    >
    > (a) In javascript, = is assignment, not comparison


    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.

    > (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


    But this is better:

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

    the chxrequest.checked shortcut is an IE-ism.
    > (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.


    No comment.



    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/
    Randy Webb, Jun 4, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mit
    Replies:
    1
    Views:
    805
    Ramu Pulipati
    Jan 25, 2006
  2. Gnarlodious
    Replies:
    4
    Views:
    17,588
    elizas
    May 5, 2010
  3. check box enable/disable

    , Dec 27, 2004, in forum: Javascript
    Replies:
    1
    Views:
    149
    Evertjan.
    Dec 27, 2004
  4. check box enable/disable

    , Jan 3, 2005, in forum: Javascript
    Replies:
    1
    Views:
    101
    Richard Cornford
    Jan 3, 2005
  5. babu17
    Replies:
    1
    Views:
    139
Loading...

Share This Page