Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Switching back/forth between background images using JS

Posted on 2013-01-14
3
Medium Priority
?
275 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 1200 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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
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…
Suggested Courses

916 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