When a cell of the table is clicked, I want to set the content of this cell but only if it is neither in the first row nor in the first column.
$('td').click(function() {
console.log($(this).parents('tr'));
console.log($('#matrix').find('tr:first'));
console.log($('#matrix').find('tr:first') == $(this).parents('tr'));
if ($(this).parents('tr') != $('#matrix').find('tr:first')) {
console.log("is ok");
if ($(this).text() != "x") { // Connect
$(this).text("x");
alert("Connecting " +
$('td:first', $(this).parents('tr')).text() +
" with " +
$('tr:first', $(this).parents('td')).text());
} else { //Disconnect
$(this).text("");
alert("Disconnecting Endpoint " +
$('td:first', $(this).parents('tr')).text() +
" with Endpoint " +
$('tr:first', $(this).parents('td')).text());
};
}; // row/columns > 0 only
}); // click()
td {
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="matrix" border="0">
<tr>
<td>Matrix</td>
<td>_name_</td>
<td>_name_</td>
</tr>
<tr>
<td>_name_</td>
<td></td>
<td></td>
</tr>
<tr>
<td>_name_</td>
<td></td>
<td></td>
</tr>
</table>
The Firefox debuging console highlights the exact same elements, but
console.log( $(this).parents('tr') == $('#matrix').find('tr:first') );
always shows false, regardless on which cell I click.
What am I doing wrong?