Jump to content

Simple Boolean If - What's Wrong Here?


Hitch

Recommended Posts

Hi:I have a classifieds CMS that can utilize variables created by field names. I created a boolean field fo users to check off if they wanted a little attention-getter icon displayed (A red "F" featured icon). The script looks like this:

<script type="text/javascript">var myVar = '{Urgent}';if (myVar = true) {document.write("<IMG ALIGN='center' " + "SRC='http://www.mysite.com/pictures/icons/featured.gif'>"); }</script>

Now, this works. FYI, the {Urgent} item is the field name/variable of the boolean checkbox. The problem is that it works whether or not the user clicks or does not click off the box. I've tested it a variety of ways, but cannot figure out why the heck this isn't working properly. If the field is unchecked, it should be false (null, iow), and the image should NOT display...but display it does.Any thoughts or insight?Thanks,Hitch

Link to comment
Share on other sites

The logical equality operator for JS (and most C-based languages) is ==, not = (which is for assignment). Also, make sure you check the value property of the field object, not the object itself, and that you can just write if (myVar) when checking for boolean equality.

Link to comment
Share on other sites

Be aware that assignment operators ARE often used in Boolean tests, but not in the way you intended. Typically it is to test the result of a function call, and maybe combine 2 statements into one statement. So you might see a thing like this:

if (val = document.getElementById("my_input").value) {   // I am simultaneously assigning the input value to a variable (val) and testing whether the value is empty/null.}

Programmers do this a lot, so it might look like a violation of Synook's point, but really it's not. It could easily be rewritten as this, and in many cases probably should be:

val = document.getElementById("my_input").value;if (val) {	// Now it's a simple Boolean test. }

Link to comment
Share on other sites

The logical equality operator for JS (and most C-based languages) is ==, not = (which is for assignment). Also, make sure you check the value property of the field object, not the object itself, and that you can just write if (myVar) when checking for boolean equality.
Thanks, I felt like an idiot when I read your post, because I do know that...just couldn't see it. Fixing it hasn't made it work, sadly; at least, not in the way intended, but I'll keep whacking at it, thank you again.@Deirdre's Dad: Thank you also for your input. I'm studying your code to see how I could incorporate it and hopefully get this (shoulda been simple!) snippet working. I've been slogging through a course at Lynda.com, but not really happy with my progress/comprehension. Anyone here have a better online tutorial/lesson plan suggestion? I've plowed through the tutorial here, but need something a little more in-depth - would be grateful for suggestions.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...