Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox stays checked ?? 1

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi all,

I have a form in which a checkbox is to be used to copy entries from some form textboxes to other textboxes. When the checkbox is to be clicked to the checked state the copy occurs. When the checkbox is clicked to uncheck it, the textboxes are to be cleared.

What happens is -- the first click works -- the box is checked and the copy action occurs. But further clicking does not work. The checkbox stays checked.

I'd guess that the first IF always runs. Here is the routine:


function namecopy() {

if (document.getElementById("chkCopyName").checked=true) {
document.getElementById("cc_firstname").value=document.getElementById("firstname").value;
document.getElementById("cc_lastname").value=document.getElementById("lastname").value;
document.getElementById("cc_address1").value=document.getElementById("address1").value;
document.getElementById("cc_address2").value=document.getElementById("address2").value;
document.getElementById("cc_city").value=document.getElementById("city").value;
document.getElementById("cc_state").value=document.getElementById("state").value;
document.getElementById("cc_zip").value=document.getElementById("zip").value;

}

if (document.getElementById("chkCopyName").checked=false {
document.getElementById("cc_firstname").value="";
document.getElementById("cc_lastname").value="";
document.getElementById("cc_address1").value="";
document.getElementById("cc_address2").value="";
document.getElementById("cc_city").value="";
document.getElementById("cc_state").value="";
document.getElementById("cc_zip").value="";

}

}

The second IF never runs.

Here is how the action is initiated:

<input type="checkbox" name="chkCopyName" ID="chkCopyName" onClick="namecopy()" />


Any ideas??

Thanks,
KB
 
=" is assign operator, "==" is compare operator.

>if (document.getElementById."chkCopyName").checked=true) {
[tt]if (document.getElementById("chkCopyName").checked[red]=[/red]=true) {[/tt]
or simply
[tt]if (document.getElementById("chkCopyName").checked) {[/tt]

>if (document.getElementById("chkCopyName").checked=false) {
[tt]if (document.getElementById("chkCopyName").checked[red]=[/red]=false) {[/tt]
or simply
[tt]if ([blue]![/blue]document.getElementById("chkCopyName").checked) {[/tt]

The second conditional can be also be spared and replaced by "} else {" structure.
 
Thanks tsuiji,

Works fine now!

Another thing I noticed is I left out a parenthesis:

if (document.getElementById("chkCopyName").checked=false {

The second if had no close )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top