Capture the value of a table cell and save to a variable via jQuery

Asked

Viewed 254 times

0

I have the following table

<table class="table">
  <tbody>
    <tr>
      <td scope="row">teste</td>
      <td id="teste" name="teste" class="teste">1</td>  
    </tr>
  </tbody>
</table>

I need to get the value of td by jQuery and save in a array, in my code have more rows to table, I summarized here

'teste' : jQuery('#teste').val()

I’m doing so but it doesn’t work.

1 answer

1


Since the element used in the selector is not an element of a form, you cannot take value with the . val(). Use

jQuery('#teste').text()

See in the code below:

console.log(jQuery("#teste").text());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<table class="table">
<tbody>
<tr>
    <td scope="row">teste</td>
     <td id="teste" name="teste" class="teste">1</td>   
</tr>
</tbody>
</table>

  • worked, thank you very much

Browser other questions tagged

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