Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-05-2006, 01:50 PM   #1
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 87
If statement doesn't work properly in JavaScript

I'm just learning to use JavaScript, and at the moment I'm working on writing a program to play noughts-and-crosses. It will use a grid of html buttons within a form (name/id="board") as the playing board. The onclick property of each of these buttons calls a function called Click, passing its element number within the form (held in the variable btn).

I was playing around with controlling the value of these buttons, depending on their existing value. I tried using the following seies of if statements, but it does not work (the first option is always executed - setting the button's value to "X"):
var checkit = document.forms["board"].elements[btn].value
	if(checkit=" ")
	else if(checkit="X")
	else if(checkit="0")
		{document.forms["board"].elements[btn].value=" ";}
However, the equivalent switch statement works perfectly, allowing me to cycle through the 3 values:
		case " ":
		case "X":
		case "0":
			document.forms["board"].elements[btn].value=" ";
Can anyone see what I may be doing wrong? Or is it a known problem?

I'm using SuSE 10.0 with Gnome 2.12, and I've tested this in both Firefox 1.5 and Konqueror 3.4.2.


Last edited by Robhogg; 03-05-2006 at 02:21 PM.
Old 03-05-2006, 03:03 PM   #2
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
in you if statement you want to use == to test.. by using only = you are assigning instead of testing and trashing your vars..
Old 03-05-2006, 03:30 PM   #3
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 87

Paint me yellow, and call me Homer

I should have spotted that. Thanks.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript doesn't work in Konquerer SLP SUSE / openSUSE 1 02-24-2006 03:08 AM
my first Javascript doesn't work in Blogger? Hosiah Programming 1 01-19-2006 12:27 AM
Javascript from AdSense not working properly linux-rulz General 2 03-29-2005 11:25 PM
How do I get javascript to work with Opera? Aerlock Linux - Software 0 11-17-2003 09:28 PM
I am lost, cant use if statement properly GridX Linux - Newbie 2 10-08-2003 05:39 PM

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration