0

I have a table with cells which share the same id="spot". I would like to change all these cells.

<table>
    <tr>
        <td id="spot">1</td>
        <td>2</td>
    </tr>
    <tr>
        <td id="spot">3</td>
        <td>4</td>
    </tr>
</table>
// 1 2
// 3 4

Please tell me the difference between $("td#spot") and $("#spot") as the following lines of code don't produce the same result.

$("#spot").each(function (item) {
    $(this).text("0");
})
// 0 2
// 3 4

$("td#spot").each(function (item) {
    $(this).text("0");
})
// 0 2
// 0 4
Michel Hua
  • 1,614
  • 2
  • 23
  • 44

1 Answers1

0

You can't have duplicate ids. So, change id to class.

And then do:

$('.spot') //to select element anywhere in document.

or

$('td.spot'); //to select a td with "spot" class.

or

$('tr > .spot'); //to select a immediate child of tr having class "spot"

or

$('table .spot'); //to select a child at any depth  having class "spot"

In the first, you are just being liberal, while in the second you are being more specific.

Also, $(this).("0") doesn't make sense. I think you wanted $(this).text("0")

Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22
Amit Joki
  • 58,320
  • 7
  • 77
  • 95