0

I have

<div class="test">hello everyone</div>

and I want to change each hello in this div with

<h1>helllo</h1>

for example I've used

$(this).text().replace("hello", "<h1>hello<h1>");

but it outputs

<h1>hello</h1>

I want it apply as HTML not as text how can I do it?

12
  • 1
    try using $(this).html() and setting it in the same manner Commented Jan 6, 2019 at 19:23
  • tried it didn't work either Commented Jan 6, 2019 at 19:24
  • Try to use the class selector? Commented Jan 6, 2019 at 19:26
  • 1
    $(this).html($(this).html().replace("hello", "<h1>hello</h1>")); Commented Jan 6, 2019 at 19:27
  • 1
    $(".code").html($(".code").html().replace("int", "<span class='var'>int</span>")); ... what you wrote should be this (without any wrapping function). Commented Jan 6, 2019 at 19:35

5 Answers 5

1

Your code is only replacing the text returned. That too it wont replace in the DOM, it will only be in javascript. So after replacing the text, render the returned value using .html()

var $myDiv = $('.test');
$myDiv.html($myDiv.text().replace("hello", "<h1>hello</h1>"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="test">hello everyone</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Answers are better when you provide explanation of the code you suggested.
This code was simple and self explanatory, hence I did not explain it. Now I have updated
0

If you are interested in a pure JavaScript approach, you can use the innerHTML() property to replace your string with html like this:

var x = document.querySelector(".test");
x.innerHTML = x.innerHTML.replace("hello", "<h1>hello</h1>");
<div class="test">hello everyone</div>

6 Comments

The backticks aren't necessary, you don't need template strings here
@SebastianSpeitel Thanks for the heads up :) Answer updated mate.
doesn't work when you are locking for two words to change
@mouhamdAgoumi It does work for a string with multiple words too. Check this jsFiddle to see how I changed two words from a string.
@mouhamdAgoumi The current solution works for that too. Check here: jsfiddle.net/AndrewL64/1gy7ukr5/14
|
0

.html() to access & change the html, regex to replace hello with <h1>hello</h1>

$(this).html($(this).html().replace(/hello/g,"<h1>helllo</h1>"));

Comments

0

Hey I wanted to highlight a code in my div but since I didn't have a lot of time to do that I just used highlight.js to do that but I'm still looking for a good idea to do that

Comments

-3

The code you have written for this replacement is perfectly nice but instead replacing a particular section, you are replacing complete div data

1.) Store div content to a variable e.g. var myDiv

2.) Replace desired content

3.) Put back replaced portion back to music

Example

jQuery(".test").html(function(){
      jQuery(this).text().replace("hello", "<h1>hello</h1>");
})

1 Comment

text() will html-encode the argument, the html will be &lt;h1&gt;hello&lt;h1&gt; - and you forgot to save the results anywhere.. and you don't use a regex to match every hello, just the first will be replaced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.