Reputation: 13
How can I solve this code error when running!
var replaced = $("body").html().replace(/<td>YES</td>/g,'<span class="yes"></span>');
$("body").html(replaced);
I want to replace all
<td>YES</td>
with
<span class="yes"></span>
Upvotes: 0
Views: 150
Reputation: 1
try to use an escape character for the closing td in your expression
var replaced = $("body").html().replace(/<td>YES<\/td>/g,'<span class="yes"></span>');
$("body").html(replaced);
Upvotes: 0
Reputation: 74645
Consider:
$("td").filter(function() {
return $(this).text() === 'YES';
}).replaceWith('<span class="yes"></span>');
This finds all the td
elements and filters for the ones with YES
as the only contents and the replaces them.
Upvotes: 1