Sanjeev K Sanjeev K - 5 months ago 38
HTML Question

Find words and replace with tag wihtin div jQuery

I have a div with some text and I am trying to replace that text with a html tag using jQuery but, its replacing tag as text not as html tag

following is the html structure

<h3>Rs.1410 / 30 Tablet(s) </h3>

JS which I tried is

$("h3").text(function () {
return $(this).text().replace("Rs.", "<i class='fa fa-inr'></i>");

If I try .html() then its replacing all the content within that div which I don't want. Can anybody please suggest



Use the html() function instead of text()

$("h3").html(function () {
    return $(this).text().replace("Rs.", "<i class='fa fa-inr'></i>"); 

From the documentation

A function returning the HTML content to set. Receives the index position of the element in the set and the old HTML value as arguments. jQuery empties the element before calling the function; use the oldhtml argument to reference the previous content. Within the function, this refers to the current element in the set.