1

Hi everyone I want to replace data inside the row depending upon the row number. The code I have posted traverses through the rows in the table, counts the row-indexes and matches them with the required row. If condition gets true the data should be replaced in that row. I have edited the question

I don't know what should I use to replace the data?

I have tried .innerHTML, .replaceWith() but they do not give me the required thing.

$('table#table-mytable tr').each(function(){
  var row_required = tag[tag.length-2];
  console.log('this is row_required: '+row_required);
  var row = $(this).index()+1;
  console.log('this is row: '+row);
  if(row==row_required) {
     (What should I enter here)
  }
});

I want replace with something like: replaceWith('td colspan="2" input type="text" name="input_" class="input_" id="input_" value="' + input_tag + '" placeholder="Enter Label" /td>')?

1
  • You are looping the rows. A row however doesn't have content. It has cells (<td>) which contain the content. So you are looking for something like row.find('td').html('The new text') Commented Nov 16, 2017 at 14:35

1 Answer 1

1

You can use html() (amongst other methods) to replace the contents of the tr. Also note that you don't need to loop at all. If you know the index you can select it directly using eq():

var row_required = 1;
$('#table-mytable tr').eq(row_required).html('<td>Bar</td>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<table id="table-mytable">
  <tbody>
    <tr><td>Foo</td></tr>
    <tr><td>Foo</td></tr>
    <tr><td>Foo</td></tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.