Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Switching back/forth between background images using JS

Posted on 2013-01-14
3
Medium Priority
?
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
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 …

705 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