Solved

Switching back/forth between background images using JS

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

803 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