PDA

View Full Version : if else statement not working


Nagnag
09-01-2006, 07:58 AM
function revealcontrol(){
var txt = document.getElementById('textbox1');

if(txt='1'){revealit1()}
else if(txt='2'){revealit2()}
else if(txt='3'){revealit3()}
else alert('test')
}

I'm not sure what I'm doing wrong, but it's not working. What I want it to do is when my textbox1.value = '1' have it call a certain function. It doesn't have to be a textbox, it can be anything that presets a value, like a listbox or whatever. This value will be set by something previously in my script, but I just can't get THIS part to work =/

kapaha
09-01-2006, 08:33 AM
var txt = document.getElementById('textbox1');

That bit should be


var txt = document.getElementById('textbox1').value;


Otherwise, you are just withdrawing the information about what the element is. I don't know if that'll fix the issue, but it is an error, so give it a go with that corrected, as at the moment, txt is always the same thing: "HTML x ELEMENT", where x is the type of element from which you are drawing the value. ;)

Nagnag
09-01-2006, 08:38 AM
Actually someone in a usenet just told me what I was doing wrong and it works!

(txt.value == '1')

Such stupid syntax =[

RysChwith
09-01-2006, 08:56 AM
Such stupid syntax =[That syntax exists for a reason. Comparison (==) is different from assignment (=), so it should have a different operator. It becomes confusing in languages where that distinction isn't apparent (VB comes to mind).

Rys

Nagnag
09-01-2006, 09:49 AM
Ahh nice, thanks I understand now ;) +1 rep to both of you