Skip to content Skip to sidebar Skip to footer

I Want To Use Javascript Replace() To Add Html Tags (but It's Returning Them As Strings)

I have a simple javascript function which replaces the 'y' character within a any span.certain_class with an 'i': $(document).ready(function() { $('span.certain_class').each(functi

Solution 1:

Try

$(this).html($(this).text().replace........

That will make the result parse as HTML rather than plain text.


Solution 2:

Use $(this).html(...) instead of .text() to set the contents. You might also want to use html when reading the contents to replace (depends on what they are in your scenario).


Solution 3:

You should be able to just change the first call to text() to a call to html():

$(this).html($(this).text().rep...

Solution 4:

$(document).ready(function() {
    $('span.certain_class').each(function() {
        $(this).html($(this).text().replace(/y/ig, function(s) {
            return (s === 'Y' ? 'I' : 'i');
        }));
    });
});

Change your $(this).text to $(this).html


Post a Comment for "I Want To Use Javascript Replace() To Add Html Tags (but It's Returning Them As Strings)"