Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Second JQuery instruction inside HTML doesn't execute

I have a simple HTML page that looks like this html page and this is the HTML code for it

<!DOCTYPE html>
<html>

<body>

<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Tincidunt a gravida non, tincidunt condimentum nullam.
Aliquam mi augue, venenatis sed dolor eget. Aliquam venenatis felis, nullam ante est, nullam mattis massa nec nunc imperdiet convallis.</h1>

</body>
</html>

What I would like to do, is highlight every occurrence of the words "dolor" and "nullam" on the page. In order to achieve this, I have embedded the following JQuery instructions inside the HTML code, like so

<!DOCTYPE html>
<html>

<body>

<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Tincidunt a gravida non, tincidunt condimentum nullam.
Aliquam mi augue, venenatis sed dolor eget. Aliquam venenatis felis, nullam ante est, nullam mattis massa nec nunc imperdiet convallis.</h1>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<script>
 $("body").html($("body").html().replace(/dolor/gi, "<mark>dolor</mark>"));
 $("body").html($("body").html().replace(/nullam/gi, "<mark>nullam</mark>"));
</script>

</body>
</html>

However, what I find is that only the first JQuery line gets executed, that is, only the word "dolor" is highlighted, and this is what I see on the page highlighted html page. What I expect to see instead, is something like this expected html page, where all occurrences of both words are highlighted. Any idea why the second JQuery line gets ignored and how I can fix it to achieve my expected result? Thank you all.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Each line of that script rewrites the entire content of the <body> element.

This includes the <script> element that is doing the rewriting.

This breaks the script.

Either:

  • Move it to the <head> and wrap it in a DOM ready event handler.
  • Change it so it only affects the relevant part of the page (e.g. add a <main> element).
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!