BoJack Horseman
BoJack Horseman

Reputation: 173

Replace Using JQuery

I have a modal form that pops-up on a button press event. The form currently appends the input value from the textbox to the end of the current cells value.

I am appending to the end of the current value like so

row.cells[1].innerHTML = row.cells[1].innerHTML + ' - ' + $('#<%=txtNewVal.ClientID %>').val();

Is it possible to take the new value $('#<%=txtNewVal.ClientID %>').val() and replace a value in the current string?

For example - let's say that row.cells[1].innerHTML = "Bilge Water And Muskrat Soup" and the $('#<%=txtNewVal.ClientID %>').val() reads Pumpkin Pie

Using JQuery could I replace Muskrat Soup with Pumpkin Pie? I.E. a replace string from the current value with the new value?

Upvotes: 0

Views: 82

Answers (1)

4b0
4b0

Reputation: 22323

Use replace and rebind table cell like this.

if ($('td').html().indexOf("Muskrat Soup") > 0) {
$('td').html($('td').html().replace("Muskrat Soup", "Pumpkin Pie"));

} else {
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Bilge Water And Muskrat Soup</td>
  </tr>
</table>

Upvotes: 1

Related Questions