<td> ABCDE </td>
$("td:contains('CD')").css("font-weight","Bold");
This will bold whole text like: ABCDE. How can I just bold text CD
like "ABCDE"?
<td> ABCDE </td>
$("td:contains('CD')").css("font-weight","Bold");
This will bold whole text like: ABCDE. How can I just bold text CD
like "ABCDE"?
Using a text replacement using HTML markup, but you need to specify the text to replace :
$("td:contains('CD')").each(function(td,item){
$(item).html($(item).html().replace("CD","<b>CD</b>"))
});
See https://jsfiddle.net/qo5zxx9a/
Another solution is to write the cell content using HTML markup:
<td>AB<span class="bold">CD</span>E</td>
Here is an example based on this answer.
function highlight(word, element) {
var rgxp = new RegExp(word, 'g')
var repl = '<span class="bold">' + word + '</span>'
element.innerHTML = element.innerHTML.replace(rgxp, repl)
}
var element = document.querySelector('td')
highlight('CD', element)
.bold {
font-weight: bold;
}
<table>
<td> ABCDE </td>
</table>