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


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>
      <meta charset = "utf-8">
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table id = "nameTable">
    <td id = "1234"></td>
    $(function () {
      $('table td'). each (function (index, val) {
        console.log (val.id);