Reputation: 25
I'm working on an assignment to fill the table data cells of a webpage with images that will on click swap position moving a "hero" tile to a "winning" tile. The onClick passes the cell location to a doClick method that will make sure the tile swap is not outside of the table. The table cell's row and column value is then passed to a swap method to trade the position of the two tiles. However when I click on row 1, col 0 of the table, the doClick line if (top != -1 && cells[top][col].innerHTML == "<img src=../images/mage.jpg>")
never evaluates to true. This only happens when passing the img scr element to the table position. This works when I just substitute a letter or number but not with the img src element. Any suggestion would be appreciated.
//This is called in the html body tag to setup the array of arrays for js
function setup() {
cells = new Array(
[
document.getElementById("cell00"),
document.getElementById("cell01"),
document.getElementById("cell02"),
document.getElementById("cell03"),
document.getElementById("cell04")
], [
document.getElementById("cell10"),
document.getElementById("cell11"),
document.getElementById("cell12"),
document.getElementById("cell13"),
document.getElementById("cell14")
], [
document.getElementById("cell20"),
document.getElementById("cell21"),
document.getElementById("cell22"),
document.getElementById("cell23"),
document.getElementById("cell24")
], [
document.getElementById("cell30"),
document.getElementById("cell31"),
document.getElementById("cell32"),
document.getElementById("cell33"),
document.getElementById("cell34")
], [
document.getElementById("cell40"),
document.getElementById("cell41"),
document.getElementById("cell42"),
document.getElementById("cell43"),
document.getElementById("cell44")
]
);
placeNumbers(); //call function to place values in cells
}
function placeNumbers() {
for (var rows = 0; rows < 5; rows++) //reads each element of number array into cells array
{
for (var cols = 0; cols < 5; cols++) {
if ((rows == 0) && (cols == 0)) {
cells[rows][cols].innerHTML = "<img src=../images/mage.jpg>"; //this holds position for hero and inserts image
cols++;
}
if ((rows == 4) && (cols == 4)) {
cells[rows][cols].innerHTML = "<img src=../images/treasure.jpg>"; //this holds position for treasure and inserts image
break;
} else {
cells[rows][cols].innerHTML = "<img src=../images/grass.jpg>";
}
}
}
}
function doClick(row, col) {
var top = row - 1;
var bottom = row + 1;
var left = col - 1;
var right = col + 1;
swapped = false;
if (top != -1 && cells[top][col].innerHTML == "<img src=../images/mage.jpg>")
swap(cells[row][col], cells[top][col]);
else if (right != 5 && cells[row][right].innerHTML == "<img src=../images/mage.jpg>")
swap(cells[row][col], cells[row][right]);
else if (bottom != 5 && cells[bottom][col].innerHTML == "<img src=../images/mage.jpg>")
swap(cells[row][col], cells[bottom][col]);
else if (left != -1 && cells[row][left].innerHTML == "<img src=../images/mage.jpg>")
swap(cells[row][col], cells[row][left]);
else alert("Illegal move.");
}
function swap(firstCell, secondCell) {
swapped = true;
secondCell.innerHTML = firstCell.innerHTML;
firstCell.innerHTML = "<img src=../images/mage.jpg>";
}
<div id="content">
<p>You can move your Champion into any surrounding empty tile by clicking on that empty tile. The only moves allowed are up, down, right, or left. Diagonal moves are not allowed. The object is to get to the end of the maze and save Azeroth. </p>
<table align="center" border="2">
<tr>
<td><span onclick="doClick(0,0)" id="cell00" /></td>
<td><span onclick="doClick(0,1)" id="cell01" /></td>
<td><span onclick="doClick(0,2)" id="cell02" /></td>
<td><span onclick="doClick(0,3)" id="cell03" /></td>
<td><span onclick="doClick(0,4)" id="cell04" /></td>
</tr>
<tr>
<td><span onclick="doClick(1,0)" id="cell10" /></td>
<td><span onclick="doClick(1,1)" id="cell11" /></td>
<td><span onclick="doClick(1,2)" id="cell12" /></td>
<td><span onclick="doClick(1,3)" id="cell13" /></td>
<td><span onclick="doClick(1,4)" id="cell14" /></td>
</tr>
<tr>
<td><span onclick="doClick(2,0)" id="cell20" /></td>
<td><span onclick="doClick(2,1)" id="cell21" /></td>
<td><span onclick="doClick(2,2)" id="cell22" /></td>
<td><span onclick="doClick(2,3)" id="cell23" /></td>
<td><span onclick="doClick(2,4)" id="cell24" /></td>
</tr>
<tr>
<td><span onclick="doClick(3,0)" id="cell30" /></td>
<td><span onclick="doClick(3,1)" id="cell31" /></td>
<td><span onclick="doClick(3,2)" id="cell32" /></td>
<td><span onclick="doClick(3,3)" id="cell33" /></td>
<td><span onclick="doClick(3,4)" id="cell34" /></td>
</tr>
<tr>
<td><span onclick="doClick(4,0)" id="cell40" /></td>
<td><span onclick="doClick(4,1)" id="cell41" /></td>
<td><span onclick="doClick(4,2)" id="cell42" /></td>
<td><span onclick="doClick(4,3)" id="cell43" /></td>
<td><span onclick="doClick(4,4)" id="cell44" /></td>
</tr>
</table>
</div>
Upvotes: 1
Views: 124
Reputation: 1511
As the edit I have done some checks
The innerHTML condition always return false so you have to log its value to check the correct value.
InnerHTML returns
<img src="https://cdn.pixabay.com/photo/2017/06/22/20/32/background-2432434_960_720.jpg" width="50">
so the condition should be
if (top != -1 && cells[top][col].innerHTML == '<img src="https://cdn.pixabay.com/photo/2017/06/22/20/32/background-2432434_960_720.jpg" width="50">'){}
You can also use \n
as @NewToJS mentioned
Upvotes: 1