Solved

Switching back/forth between background images using JS

Posted on 2013-01-14
3
266 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

685 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