I need to recover the title of the selected field in jquery!

Asked

Viewed 66 times

-4

For example by clicking on the td 'Silva', display the title "Last name"

<pre>
<table>
  <tr>
    <th class='title'>Nome</th>
    <th class='title'>Sobrenome</th>
    <th class='title'>Idade</th>
  </tr>
  <tr>
    <td class='campo'>Carlos</td>
    <td class='campo'>Borges</td>
    <td class='campo'>50</td>
  </tr> <tr>
    <td class='campo'>Joao</td>
    <td class='campo'>Silva</td>
    <td class='campo'>22</td>
  </tr>
</table>
</pre>
  • Post part of the code that we can help more easily and clarify the explanation.

  • You have not given enough information to answer your question.

  • is the first time I use the stack guys, I’m beginner in the area, the code I haven’t got yet, so this problem I need to solve.

  • I’ve already edited, if anyone can help

1 answer

0


Follow an example using Jquery.

$(document).ready(function() {
  $('td').click(function() {
    var titulo = $(this).closest('table').find('th').eq($(this).index()).text();
    alert(titulo);
  });
});
pre {
  font-size: 1.5em;
}

table {
  border-spacing: 10px;
  border-collapse: separate;
  min-width: 50px;
}

th {
  text-align: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre>
<table>
  <tr>
    <th class='title'>Nome</th>
    <th class='title'>Sobrenome</th>
    <th class='title'>Idade</th>
  </tr>
  <tr>
    <td class='campo'>Carlos</td>
    <td class='campo'>Borges</td>
    <td class='campo'>50</td>
  </tr> <tr>
    <td class='campo'>Joao</td>
    <td class='campo'>Silva</td>
    <td class='campo'>22</td>
  </tr>
</table>
</pre>

  • mtto bom, vlw Leandro

  • If solved, please mark as answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.