Harugawa Harugawa - 5 months ago 12
Javascript Question

Finding a certain number in html and replace it with custom text

So, I have a table and there is a colum called value with different numbers that varies from 0 to 2.

<column>Values<column>
<td>0</td>
<td>2</td>
<td>0</td>


Yes, I know that something like does not exist, it was just for sake of example.
But the real question is, how can I with help of jquery find those numbers and replace it with something else.. for example

if(td == 1){
replace 1 with "Hello"
}

Answer

Use contains() to get td which have similar text and replace text using .text()

$("td:contains('1')").text("hello");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>