Home>

I want to get the id in the table, but it is undifinde.

<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>

I want to get"1234"
$("table # nameTable"). attr ("td # id")
I tried to write it like this, but it didn't work.
Please give me advice.

  • Answer # 1

    Well there is only one td in this case

    console.log ($('# nameTable td'). attr ('id'));


    If you want to pick up all ids

    console.log ($('# nameTable td [id]'). map (function () {
      return $(this) .attr ('id');
    }). get ());

  • Answer # 2

    Assuming id is attached to the first td using jQuery

    $('table # nameTable td: first'). attr ('id')

  • Answer # 3

    It looks like this

    <! DOCTYPE html>
    <html>
    <head>
      <meta charset = "utf-8">
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
    <table id = "nameTable">
    <tbody>
    <tr>
    <td id = "1234"></td>
    <td></td>
    </tr>
    </tbody>
    </table>
    <script>
    $(function () {
      $('table td'). each (function (index, val) {
        console.log (val.id);
      });
    });
    </script>
    </body>
    </html>