Exchange td value after validation

Asked

Viewed 132 times

1

I have the following HTML code

<table>
<tr>
<td class="x">10</td>
<td class="x">10</td>
<td class="x">12</td>
<td class="x">18</td>
</tr>
</table>

Through jquery I can pick up and verify what the value inside the <td> using the following function:

$(".x").text(function(index, item){
console.log(item);
{

How do I check if <td> has the value 10, and thus exchange the value 10 to a string, and then play into the <td> that has the value 10 back with the value changed?

3 answers

2


Try it this way:

$('#appendButton').click(function(){
    $( ".x" ).each(function( index ) {
  if($(this).text() === "10"){
  $(this).html("VDC");
  }
});
});

1

You can do something more or less like this:

if (parseInt(item.innerHTML) === 10) {
  var novoValor = 50; // Implemente aqui a lógica do novo valor
  item.innerHTML = novoValor;
}

You can get the content inside the div using the innerHTML.

  • parseint($(".x")[index].innerHTML) === 10 ... $(".x")[index]. innerHTML = newValue

0

Don’t forget to add an ID to improve the element identification. But I believe whatever you want is something like this:

$(function(){ // just jquery init
  $('table').find('td').each(function (index, item) {
    var itemContent = $(this).html()
    console.log( itemContent );
    if ( itemContent == 10 || itemContent == '10' ){
        // do something here
       console.log( item, index );
    }
  });
})

Browser other questions tagged

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