How do I get value in the <td> tag in jquery?

Asked

Viewed 394 times

1

How do I get the value 1 and manipulate - lo, by jquery, which is inside the td tag (I want this value to increase as I click an id button = 'add-Row'):

 <td>1</td>     
  • I don’t understand, could be more specific?

  • sorry, forgot q the html tag was interpreted haha

  • 1

    You can use $('td').html() to read and $('td').html('novo valor') to change... is that your question? If you explain your HTML better it is easier if only $('td') go get the td all.

  • I think yes, I will test..

2 answers

2


You can use $('td').html() to read and $('td').html('novo valor') to change...

Depending on your HTML you should be more specific in the selector $('td') because this way you will select the td all. To increase the value each time an element is clicked you need to use an event receiver, and I suggest you keep that count in Javascript in the example below. But without knowing the rest of the code I can’t guess much more.

var contador = 0;
$('#add-row').on('click', function(){
    contador++; // somar +1
    $('td').html(contador);
});

Example: https://jsfiddle.net/tmx7bo52/

1

You can use custom filter. See:

var text = $("td").filter(function(){
    return $(this).text() === "I";
}).text();

console.log("Text: " + text)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table><tr><td>I</td></tr></table>

Browser other questions tagged

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