Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / October 2007



Tip: Looking for answers? Try searching our database.

checking and unckecking a checkbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Willie - 29 Oct 2007 17:11 GMT
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)">
Martin Honnen - 29 Oct 2007 17:38 GMT
>   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;

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Willie - 29 Oct 2007 18:01 GMT
Thanks, that was simple.

>>   checkbox.checked = 'true';
>>  }
[quoted text clipped - 6 lines]
> or
>   checkbox.checked = false;
David Cox - 30 Oct 2007 14:26 GMT
>>   checkbox.checked = 'true';
>>  }
[quoted text clipped - 6 lines]
> or
>   checkbox.checked = false;

can js do something like:

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

?
David F. Cox
Tim Streater - 30 Oct 2007 13:51 GMT
> >>   checkbox.checked = 'true';
> >>  }
[quoted text clipped - 10 lines]
>
> 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.
Doug Miller - 30 Oct 2007 14:48 GMT
>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 != "");  

Signature

Regards,
       Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.

Doug Miller - 29 Oct 2007 20:43 GMT
> text = text.value.trim();
>  if(text.value != ""){
[quoted text clipped - 3 lines]
>  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 != "");

Signature

Regards,
       Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.

David Cox - 30 Oct 2007 14:52 GMT
>> text = text.value.trim();
>>  if(text.value != ""){
[quoted text clipped - 22 lines]
>
> checkbox.checked = (text.value != "");

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
Tim Streater - 30 Oct 2007 13:52 GMT
> >> text = text.value.trim();
> >>  if(text.value != ""){
[quoted text clipped - 24 lines]
>
> 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.
Dr J R Stockton - 29 Oct 2007 21:57 GMT
>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.
[quoted text clipped - 5 lines]
>        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.

Signature

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.