1

I've written some jQuery that loops through all elements within a div and replaces and styles a specific string by just using <em> and <strong> tags, which works fine and the code is this:

var regex = /some text/gi;
$(".container *").each(function () {
    $(this).html($(this).html().replace(regex, '<em><strong>Some</strong> Text</em>'));
});

The problem I have is that this also affects the text in all the Title and Alt attributes, which I don't want because they can't be styled and literally end up being displayed as <em><strong>Some</strong> Text</em>.

I've tried storing the original Title/Alt values in variables before they're replaced then setting them back to their original values afterwards, but it seems that when the attribute values include the <em> and <strong> tags they are completely inaccessible. I can't even remove them and add them back in, which seems really strange.

Can anyone tell me if there is any way around this please?

AlanPartridge
  • 63
  • 1
  • 9

1 Answers1

0

Instead of $(this).html().replace, have you tried $(this).text().replace?

This way you only replace the actual text of the elements rather than all of the html.

Anand Shah
  • 180
  • 5