LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

Rep: Reputation: 85
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"):
Code:
var checkit = document.forms["board"].elements[btn].value
	if(checkit=" ")
		{document.forms["board"].elements[btn].value="X";}
	else if(checkit="X")
		{document.forms["board"].elements[btn].value="0";}
	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:
Code:
switch(document.forms["board"].elements[btn].value)
	{
		case " ":
			document.forms["board"].elements[btn].value="X";
			break;
		case "X":
			document.forms["board"].elements[btn].value="0";
			break;
		case "0":
			document.forms["board"].elements[btn].value=" ";
			break;
	}
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.

Thanks,
Rob

Last edited by Robhogg; 03-05-2006 at 03:21 PM.
 
Old 03-05-2006, 04:03 PM   #2
xhi
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, 04:30 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 85
D'Oh!!

Paint me yellow, and call me Homer

I should have spotted that. Thanks.

Rob
 
  


Reply


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/Novell 1 02-24-2006 04:08 AM
my first Javascript doesn't work in Blogger? Hosiah Programming 1 01-19-2006 01:27 AM
Javascript from AdSense not working properly linux-rulz General 2 03-30-2005 12:25 AM
How do I get javascript to work with Opera? Aerlock Linux - Software 0 11-17-2003 10:28 PM
I am lost, cant use if statement properly GridX Linux - Newbie 2 10-08-2003 06:39 PM


All times are GMT -5. The time now is 09:49 AM.

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