Check box and text box disable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim

    Check box and text box disable

    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="chxreques t"
    onclick="javasc ript:enableFiel d()" value="ON" style="float: "left" >
    <script language="javas cript">

    function enableField()
    {
    if (chxrequest.che cked)= true
    {
    document.frmcal lreport.txtrequ est.disabled= false;
    }
    if (chxrequest.che cked)= false
    {
    document.frmcal lreport.txtrequ est.disabled= true;

    }

    } </script>

    <input type = "text" id="txtrequest " name="txtreques t" size="20"
    disabled></td>
  • Lee

    #2
    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="chxreques t"
    onclick="this.f orm.txtrequest. disabled=!this. checked"
    value="ON"
    style="float:le ft">

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

    Comment

    • Dr John Stockton

      #3
      Re: Check box and text box disable

      JRS: In article <729757f9.04060 31227.10a67aff@ posting.google. com>, seen
      in news:comp.lang. javascript, Jim <jim.ferris@mot orola.com> posted at
      Thu, 3 Jun 2004 13:27:04 :
      [color=blue]
      >function enableField()
      >{
      >if (chxrequest.che cked)= true
      >{
      >document.frmca llreport.txtreq uest.disabled= false;
      >}
      > if (chxrequest.che cked)= false
      > {
      > document.frmcal lreport.txtrequ est.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.frmcal lreport.txtrequ est.disabled = ! chxrequest.chec ked
      (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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • Randy Webb

        #4
        Re: Check box and text box disable

        Dr John Stockton wrote:
        [color=blue]
        > JRS: In article <729757f9.04060 31227.10a67aff@ posting.google. com>, seen
        > in news:comp.lang. javascript, Jim <jim.ferris@mot orola.com> posted at
        > Thu, 3 Jun 2004 13:27:04 :
        >
        >[color=green]
        >>function enableField()
        >>{
        >>if (chxrequest.che cked)= true
        >>{
        >>document.frmc allreport.txtre quest.disabled= false;
        >>}
        >>if (chxrequest.che cked)= false
        >>{
        >>document.frmc allreport.txtre quest.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="javas cript1.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.frmcal lreport.txtrequ est.disabled = ! chxrequest.chec ked[/color]

        But this is better:

        document.frmcal lreport.txtrequ est.disabled =
        !document.frmca llreport.chxreq uest.checked

        the chxrequest.chec ked 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.javas cript FAQ - http://jibbering.com/faq/

        Comment

        Working...