Select information from a row in an HTML table

1

I have the following code:

var clicado = null;

$('.clicado').click(function(){
  clicado = $(this).val();
  $('#mostrarId').html(clicado);  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<table class="table table-hover table-striped">
  <tr>
    <td>1</td>
    <td>Primeiro</td>
    <td>
      <button type="button" value="1" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
  <tr>
    <td>2</td>
    <td>Segundo</td>
    <td>
      <button type="button" value="2" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
  <tr>
    <td>3</td>
    <td>Terceiro</td>
    <td>
      <button type="button" value="3" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
</table>
<div class="modal fade" id="mostrar">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Mostrar</h4>
      </div>
      <div class="modal-body">
        <p>Mostrar esse registro?</p>
        <table>
          <tr>
            <td id="mostrarId"></td>
          </tr>
        </table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="confirmar">Confirmar</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
      </div>
    </div>
  </div>
</div>

When I click on the show button it opens the modal and shows the number of the index that was clicked, how do I show the name, that is, when I click on the Show button of the index 1 I would like to show only line 1 in the modal with the index and the name that would be "First"?

    
asked by anonymous 23.09.2017 / 20:11

1 answer

2

In a simple way, you can search for <td> with index 1 ( eq(1) ) within the line to which the button that was clicked belongs.

var clicado = null, nome = null;
$('.clicado').click(function(){
  nome = $(this).parents('tr').find('td').eq(1).text();
  clicado = $(this).val();
  $('#mostrarId').html(clicado+ ', ' +nome);  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<table class="table table-hover table-striped">
  <tr>
    <td>1</td>
    <td>Primeiro</td>
    <td>
      <button type="button" value="1" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
  <tr>
    <td>2</td>
    <td>Segundo</td>
    <td>
      <button type="button" value="2" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
  <tr>
    <td>3</td>
    <td>Terceiro</td>
    <td>
      <button type="button" value="3" class="btn btn-primary clicado" data-toggle="modal" data-target="#mostrar">Mostrar</button>
    </td>
  </tr>
</table>
<div class="modal fade" id="mostrar">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Mostrar</h4>
      </div>
      <div class="modal-body">
        <p>Mostrar esse registro?</p>
        <table>
          <tr>
            <td id="mostrarId"></td>
          </tr>
        </table>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal" id="confirmar">Confirmar</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
      </div>
    </div>
  </div>
</div>
    
23.09.2017 / 20:14