Jquery test table [closed]

1

When clicking on line 1 on any button it returns me the value just below the article, if I click on the same line on a different button it replaces the value, if I click on line 2 it returns the value and puts it just below the other value?

<!DOCTYPE HTML>

<html>

<head>
  <title>Untitled</title>
</head>

<body>
    <table>
<tr><th>Linha 1</th>
  <td> <button>valor 01</button> </td>
  <td><button>valor 02</button> </td>
  <td><button>valor 03</button> </td>
  <td><button>valor 04</button> </td>
</tr>
<tr><th>Linha 2</th>
  <td> <button>valor 05</button> </td>
  <td><button>valor 06</button> </td>
  <td><button>valor 07</button> </td>
  <td><button>valor 08</button> </td>
</tr>
<tr><th>Linha 3</th>
  <td> <button>valor 09</button> </td>
  <td><button>valor 10</button> </td>
  <td><button>valor 11</button> </td>
  <td><button>valor 12</button> </td>
</tr>
<tr><th>Linha 5</th>
  <td> <button>valor 13</button> </td>
  <td><button>valor 14</button> </td>
  <td><button>valor 15</button> </td>
  <td><button>valor 16</button> </td>
</tr>
</table>
</body>
<section id="Valor"> <legend>Valores Obtidos</legend>
   <article>
        <!--- Aqui fica os valores obtidos dos button--->
   </article>
</section>
</html>
    
asked by anonymous 30.04.2016 / 17:32

1 answer

2

To get the text of the button clicked and the value of the "Line" that you put there you could do something like:

    $("button").click(function(e){
          e.preventDefault();

          // pega o texto do elemento clicado
          var buttonText = $(this).text(); 

          // pega o texto do elemento <th> mais próximo
          var thText = $(this).closest('th').text(); 

          // atribui valor ao article
          $("#valor article").append("<p>Botao: " + buttonText + " Linha: "+ thText +"</p>");
    });

You used the stack to publish your code, but edit and post your question, so more people find the answer, hug.

    
30.04.2016 / 18:00