I'm creating a Chrome Extension to highlight the user's name on the page. I am able to detect the name on the page, but I am having difficulty in actually highlighting just the user's name instead of the entire span it is in. Below name_to_highlight is a variable holding a string representation of the name identified.
$( "span:contains('" + name_to_highlight + "')" ).contents().wrap( "<mark></mark>");
EDIT: Thanks to the response, I was able to alter the code so it was applicable to any web page.
// only highlight the child element containing name
var targets = $( "*:contains('" + name_to_highlight + "')" );
for(var i = 0; i < targets.length; i++ ){
if(targets[i].children.length < 1){
var spanText = targets[i].innerHTML;
// wrapping a name class around the date
targets[i].innerHTML = (spanText.replace(name_to_highlight, "<mark>" +
name_to_highlight + "</mark>"));
}
}