jquery test table

Asked

Viewed 41 times

1

By clicking on line 1 on any button it returns the value just below in 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 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>

  • And what is your doubt?

  • Put your question, I know you posted it in the group, but if it’s just to put the code somewhere, look for something specific to it. it’s nice you share the doubt here so the answer comes to more people, hug.

  • When I click on line 1 on any button it returns the value just below in 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 just below the other value? @Fleuquer Lima

  • edits the post and asks the question before the code.

1 answer

2


To get the text of the button clicked and the value of the "Line" 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 put your doubt, so more people find the answer, hug.

Browser other questions tagged

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