Solved

Switching back/forth between background images using JS

Posted on 2013-01-14
3
258 Views
Last Modified: 2013-01-14
I am having a bit of trouble getting a small Keno project working properly. I have gotten the code to work properly when selecting a chosen cell, but I am unable to deselect the cell and change the background property of the <td> to the default attribute. See code below:
<script type="text/javascript">
<!--
function selected(count) {
    if (kenoBack[count] = 'images/keno/kenoSelect.png')	{
		kenoBack[count] = 'images/keno/kenoBlank.png';
		d=document.getElementById(count);
		d.setAttribute('background',kenoBack[count]);
	}
//	else	{
		kenoBack[count] = 'images/keno/kenoSelect.png';
		d=document.getElementById(count);
		d.setAttribute('background',kenoBack[count]);
//	}
}
//-->

var count=1;
var kenoBack = new Array();
document.write('<table>');
document.write('<tr>');
for (row=1;row<=8;row++)	{
	for (cell=1;cell<=10;cell++)	{
		kenoBack[count] = 'images/keno/kenoBlank.png';
		document.write('<td background="'+kenoBack[count]+'" onclick="selected('+count+')" id="'+count+'">');
		document.write(count);
		document.write('</td>');
		count++;
	}
	document.write('</tr>');
}
document.write('</table>');
</script>

Open in new window

If i uncomment lines 9 & 13, the whole thing fails to work properly at all. Right now, I have it where if the cell is clicked (onclick event) it will change the background property to a new value, but I can't figure out how to switch it back on a second click of the cell.

Any hints of where to go with this?
0
Comment
Question by:wadehults
3 Comments
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 300 total points
ID: 38775871
I simplified your script. You really didn't need that array. If you were using an array because you're concerned about performance, then you really need to rwrite the whole thing without using document.write()

     
<script type="text/javascript">
            function selected(count) {
                var d = document.getElementById(count);
                if (d.getAttribute("background") == 'images/keno/kenoSelect.png'){
                    d.setAttribute('background','images/keno/kenoBlank.png');
                }else{
                    d.setAttribute('background','images/keno/kenoSelect.png');
                }
            }


            document.write('<table>');
            var count = 1;
            for (var row=1;row<=8;row++){
                document.write('<tr>');
                for (var cell=1;cell<=10;cell++){
                    document.write('<td background="images/keno/kenoBlank.png" onclick="selected('+count+')" id="'+count+'">');
                    document.write(count);
                    document.write('</td>');
                    count++;
                }
                document.write('</tr>');
            }
            document.write('</table>');
        </script>

Open in new window

0
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 200 total points
ID: 38775935
looks like this line:

if (kenoBack[count] = 'images/keno/kenoSelect.png')      {

should be:

if (kenoBack[count] == 'images/keno/kenoSelect.png')      {

Cd&
0
 
LVL 3

Author Closing Comment

by:wadehults
ID: 38776027
Thanks for the help, both of you. I believe the comparison operator was the key. I should have been using == instead of =.

As far as the array goes, I was going to use that as a later expansion to store values for comparison, but I am sure I can come up with a different method to do that.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now