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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with if-condition

Status
Not open for further replies.

BennySHS

Programmer
Mar 15, 2005
32
DE
Hi there,

every few month I need a little javascript, and every time I get into problems :/
I hope you can explain this to me:

I want to write a small functions which set some elements visible / invisible, dependent if the related checkbox is checked or not.
Should be very simple, but sadly not for me.

The ideea:

function test()
{
if (document.getElementById('zuteiler').checked = true)
{
// set the elements visible
}
// hide the elements
}


But this doesnt work. With this function I event can't uncheck my checkbox even more...

It seems to me that javascript makes out of the comparison "if (document.getElementById('zuteiler').checked = true)" a command and set allways the chechbox to checked ??!

I hope you can help me once more,
thx a lot,
greets ben
 
Your problem is that Javascript uses a DOUBLE equal sign in an if condition. Your statement SHOULD be:

if (document.getElementById('zuteiler').checked =[red]=[/red] true)

Lee
 
thx for your annotation, but that's not the reason :/
 
BennySHS
trollacious is correct that if you are using a single equal sign it will treat it as an assignment rather than a comparison. With two equal signs it will evaluate properly unless there are other problems with your code.

You do not even need the equal sign though.
(document.getElementById('zuteiler').checked) is a statement in itself that returns true or false based on the status of the checkbox.
if (document.getElementById('zuteiler').checked)
{
alert("It was checked");
}
else
{
alert("It was not checked");
}
The above code evaluates the field and responds correctly. It does nothing to SET the checked property, only to test it and respond.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top