0
<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"?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
張哲懷
  • 3
  • 1
  • 2
    You can only set styling on a whole text node. If you want to style part of it you need to split it in to separate nodes, by wrapping the part you want to make bold in a `span`, for example – Rory McCrossan Aug 17 '17 at 15:59

2 Answers2

0

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>
F.Igor
  • 4,119
  • 1
  • 18
  • 26
0

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>
Ikbel
  • 7,721
  • 3
  • 34
  • 45