View Full Version : If statement proving true when it shouldn't.

03-11-2004, 11:34 PM
I am working on a page that has it's value read in through a database. One of the form's hidden fields is name "productName". In this field a value is imported from the database. I know this portion works becase I've use it on countless other pages and I display this field in another area of the page and the names are all read correctly.

I am trying to test against the value of this field and execute a section of code if it's true. Here is what I have.

var sel = document.toppings;
alert (sel.prodcutName.value);

If (sel.productName.value = "Calzone')
alert (" I am executing");
//do this stuff

Now if the first alerts shows "Calzone" then the second alert should show. The 2nd alert shows every single time regardless of what the first alert shows. This leads me to believe my if statement is not checking the value correctly and it probably has something to do with the quotes. I've tried using double " " on each side, but dreamweaver always replaces the 2nd " with '.

Help? :)

03-11-2004, 11:43 PM

Try changing

If (sel.productName.value = "Calzone')


If (sel.productName.value = "Calzone" )

You can use either syntax, (') or (") but you can't mix and match.
Hope that helps,

03-11-2004, 11:51 PM
I am using Dreamweaver MX and any time you use " " to surround a string, it changes the outter " to '.

I opened up the file in notepad and it's showing "Calzone" instead of "Calzone' so it must just be a DW thing.

Thanks for the help, but it's still not working. :(

03-12-2004, 12:00 AM
If (sel.productName.value == 'Calzone')

The double equal signs seem to work though I am not sure what they do. But it works so that's good. I'm off to figure out more about this equal sign phenominon. :)

03-12-2004, 07:35 AM
1 = sign is used to assign values to the variable to the left of it,

2 = signs are used to test whether the left and rigth side are of the same value.

03-12-2004, 08:31 AM
Thanks for that little tidbit of information. That should prove to be most usefull. :)