checking and unckecking a checkbox

W

Willie

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)">
 
M

Martin Honnen

Willie said:
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;
 
W

Willie

Thanks, that was simple.

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

Doug Miller

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 != "");
 
D

Dr J R Stockton

Mon said:
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.
 
T

Tim Streater

David Cox said:
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.
 
T

Tim Streater

David Cox said:

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

David Cox

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

can js do something like:

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

?
David F. Cox
 
D

Doug Miller

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 != "");
 
D

David Cox

Doug Miller said:
'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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top