LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 01:50 PM   #1
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
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 02:21 PM.
 
Old 03-05-2006, 03: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, 03:30 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

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

Paint me yellow, and call me Homer

I should have spotted that. Thanks.

Rob
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:38 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
Open Source Consulting | Domain Registration