checking and unckecking a checkbox

Discussion in 'Javascript' started by Willie, Oct 29, 2007.

  1. Willie

    Willie Guest

    I have a script that I want to check the checkbox if something is writen in
    a text field. Then uncheck it if it erased.
    The check works but not the uncheck.
    Here is the code:
    String.prototype.trim =
    function()
    {
    return this.replace(/^\s+|\s+$/g,'');
    }
    function mark(fn, textfield, checkbox){
    var check = (document.forms[fn][checkbox]);
    var text = (document.forms[fn][textfield]);

    text = text.value.trim();
    if(text.value != ""){
    checkbox.checked = 'true';
    }
    if(text.value == ""){
    checkbox.checked = 'false';
    }
    }

    in the form :
    <input type="text" name="RepID"
    onChange="mark('f1','RepID',this.form.checkbox)">
     
    Willie, Oct 29, 2007
    #1
    1. Advertising

  2. Willie wrote:

    > checkbox.checked = 'true';
    > }
    > if(text.value == ""){
    > checkbox.checked = 'false';


    checked is a boolean property so you should assign boolean values not
    strings e.g.
    checkbox.checked = true;
    or
    checkbox.checked = false;


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Oct 29, 2007
    #2
    1. Advertising

  3. Willie

    Willie Guest

    Thanks, that was simple.

    "Martin Honnen" <> wrote in message
    news:47260c9d$0$27121$-online.net...
    > Willie wrote:
    >
    >> checkbox.checked = 'true';
    >> }
    >> if(text.value == ""){
    >> checkbox.checked = 'false';

    >
    > checked is a boolean property so you should assign boolean values not
    > strings e.g.
    > checkbox.checked = true;
    > or
    > checkbox.checked = false;
    >
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
     
    Willie, Oct 29, 2007
    #3
  4. Willie

    Doug Miller Guest

    In article <>, "Willie" <> wrote:

    > text = text.value.trim();
    > if(text.value != ""){
    > checkbox.checked = 'true';
    > }
    > if(text.value == ""){
    > checkbox.checked = 'false';
    > }


    'true' and 'false' are strings. <element>.checked is a Boolean property. Any
    non-null string, when interpreted as Boolean, is true; only the null string ""
    is false. Thus when you assign the value 'false' to checkbox.checked, it
    receives the Boolean value true.

    Also, your second 'if' is pointless; simpler is

    if (text.value != "") {
    checkbox.checked = true;
    } else {
    checkbox.checked = false;
    }

    or, simpler still,

    checkbox.checked = (text.value != "");

    --
    Regards,
    Doug Miller (alphageek at milmac dot com)

    It's time to throw all their damned tea in the harbor again.
     
    Doug Miller, Oct 29, 2007
    #4
  5. In comp.lang.javascript message <>, Mon, 29
    Oct 2007 12:11:29, Willie <> posted:
    >I have a script that I want to check the checkbox if something is writen in
    >a text field. Then uncheck it if it erased.
    >The check works but not the uncheck.
    >Here is the code:
    > String.prototype.trim =
    > function()
    > {
    > return this.replace(/^\s+|\s+$/g,'');
    > }


    Not needed.

    >function mark(fn, textfield, checkbox){
    > var check = (document.forms[fn][checkbox]);
    > var text = (document.forms[fn][textfield]);


    check.checked = /\S/.test(text.value)
    }

    It will be checked if a non-whitespace character is found in the string;
    otherwise cleared.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <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 J R Stockton, Oct 29, 2007
    #5
  6. Willie

    Tim Streater Guest

    In article <aiGVi.115446$>,
    "David Cox" <> wrote:

    > "Martin Honnen" <> wrote in message
    > news:47260c9d$0$27121$-online.net...
    > > Willie wrote:
    > >
    > >> checkbox.checked = 'true';
    > >> }
    > >> if(text.value == ""){
    > >> checkbox.checked = 'false';

    > >
    > > checked is a boolean property so you should assign boolean values not
    > > strings e.g.
    > > checkbox.checked = true;
    > > or
    > > checkbox.checked = false;
    > >
    > >
    > > --
    > >
    > > Martin Honnen
    > > http://JavaScript.FAQTs.com/

    >
    > can js do something like:
    >
    > checkbox.checked = (text.value == "");


    Yes but personally I find such constructs to be unreadable; I spend 30
    mins figuring out what it means. I would put:

    checkbox.checked = true;
    if (text.value == "") checkbox.checked = false;

    This is because I have a simple mind. I am not impressed by the
    programmer being a smartypants. I am impressed by neat and logical
    layout that allows you to figure out quickly what is going on. Useful
    when it comes to maintaining someone else's code.
     
    Tim Streater, Oct 30, 2007
    #6
  7. Willie

    Tim Streater Guest

    In article <gGGVi.115454$>,
    "David Cox" <> wrote:

    > "Doug Miller" <> wrote in message
    > news:NJqVi.1937$...
    > > In article <>, "Willie"
    > > <> wrote:
    > >
    > >> text = text.value.trim();
    > >> if(text.value != ""){
    > >> checkbox.checked = 'true';
    > >> }
    > >> if(text.value == ""){
    > >> checkbox.checked = 'false';
    > >> }

    > >
    > > 'true' and 'false' are strings. <element>.checked is a Boolean property.
    > > Any
    > > non-null string, when interpreted as Boolean, is true; only the null
    > > string ""
    > > is false. Thus when you assign the value 'false' to checkbox.checked, it
    > > receives the Boolean value true.
    > >
    > > Also, your second 'if' is pointless; simpler is
    > >
    > > if (text.value != "") {
    > > checkbox.checked = true;
    > > } else {
    > > checkbox.checked = false;
    > > }
    > >
    > > or, simpler still,
    > >
    > > checkbox.checked = (text.value != "");
    > >
    > > --
    > > Regards,
    > > Doug Miller (alphageek at milmac dot com)
    > >
    > > It's time to throw all their damned tea in the harbor again.

    >
    > It is.


    Well, if it's only into the harbor we're probably OK. If he throws it
    into the harbour, on the other hand, then we're in trouble.
     
    Tim Streater, Oct 30, 2007
    #7
  8. Willie

    David Cox Guest

    "Martin Honnen" <> wrote in message
    news:47260c9d$0$27121$-online.net...
    > Willie wrote:
    >
    >> checkbox.checked = 'true';
    >> }
    >> if(text.value == ""){
    >> checkbox.checked = 'false';

    >
    > checked is a boolean property so you should assign boolean values not
    > strings e.g.
    > checkbox.checked = true;
    > or
    > checkbox.checked = false;
    >
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    can js do something like:

    checkbox.checked = (text.value == "");

    ?
    David F. Cox
     
    David Cox, Oct 30, 2007
    #8
  9. Willie

    Doug Miller Guest

    In article <aiGVi.115446$>, "David Cox" <> wrote:

    >can js do something like:
    >
    >checkbox.checked = (text.value == "");


    Yes, it can. See my earlier post in this thread. What the OP wants in this
    case, though, is

    checkbox.checked = (text.value != "");



    --
    Regards,
    Doug Miller (alphageek at milmac dot com)

    It's time to throw all their damned tea in the harbor again.
     
    Doug Miller, Oct 30, 2007
    #9
  10. Willie

    David Cox Guest

    "Doug Miller" <> wrote in message
    news:NJqVi.1937$...
    > In article <>, "Willie"
    > <> wrote:
    >
    >> text = text.value.trim();
    >> if(text.value != ""){
    >> checkbox.checked = 'true';
    >> }
    >> if(text.value == ""){
    >> checkbox.checked = 'false';
    >> }

    >
    > 'true' and 'false' are strings. <element>.checked is a Boolean property.
    > Any
    > non-null string, when interpreted as Boolean, is true; only the null
    > string ""
    > is false. Thus when you assign the value 'false' to checkbox.checked, it
    > receives the Boolean value true.
    >
    > Also, your second 'if' is pointless; simpler is
    >
    > if (text.value != "") {
    > checkbox.checked = true;
    > } else {
    > checkbox.checked = false;
    > }
    >
    > or, simpler still,
    >
    > checkbox.checked = (text.value != "");
    >
    > --
    > Regards,
    > Doug Miller (alphageek at milmac dot com)
    >
    > It's time to throw all their damned tea in the harbor again.


    It is.

    I must read the entire thread before posting.

    I must read the FAQ before posting.

    I must read my own post before posting.

    I must remember I said all that last time. :-<

    David F. Cox
     
    David Cox, Oct 30, 2007
    #10
    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. Moe Sizlak
    Replies:
    3
    Views:
    511
    Mike Smith
    Jul 20, 2004
  2. Maziar Aflatoun

    asp checkbox checking for OnCheckedChanged value

    Maziar Aflatoun, Sep 5, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    23,406
    radu_m
    Dec 29, 2008
  3. Jim Van Abbema

    Pre-checking checkbox on an orderform

    Jim Van Abbema, Dec 9, 2004, in forum: HTML
    Replies:
    2
    Views:
    693
    Mark Parnell
    Dec 9, 2004
  4. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    867
    David Wier
    Oct 6, 2006
  5. Jason Huang
    Replies:
    0
    Views:
    585
    Jason Huang
    Jun 29, 2007
Loading...

Share This Page