Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9157491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:01:48+00:00 2026-06-17T13:01:48+00:00

I m new to javascript and would be great if you could help me

  • 0

I m new to javascript and would be great if you could help me with this issue I m facing.
In the HTML code below, I m trying to highlight a particular word(say “message”)by replacing the word by appending to it to make it bold.

<body>
  <img src="http://some.web.site/image.jpg" title="abcd" />
  This is a test message.
</body>

The String “This is a test message” is found directly in the body element(so there is no id, and hence no getElementById can be used to extract the text).

So I got the entire body element and extracted the text by using textContent.(this gave me the text and ignored the image that is above the text in the html.)
Then after highlighting the word I set the ‘body’s textContent back to the new String.

The problem is that now I m not able to preserve the image that was above the text, and the new body has only the value of the textContent in it, and the image is lost. This is the JS I used(but now I m replacing message with the word phone).

<script>
function myFunction(){
  var x = document.getElementsByTagName('body')[0].textContent;
  var v = x.replace("message","phone");
  document.getElementsByTagName('body')[0].textContent = v;
} 
</script>

Is there any other way to replace text that is placed directly under the body, which has other elements too?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:01:50+00:00Added an answer on June 17, 2026 at 1:01 pm

    In order to do something like this, you would need to loop through all text nodes in the document, search for the word, and wrap it.

    Something like this:

    function highlight(str,node) {
        if( !node) node = document.body;
        var c = node.children, l = c.length, i, p;
        for( i=0; i<l; i++) {
            if( c[i].nodeType == 1) highlight(str,c[i]);
            if( c[i].nodeType == 3) {
                if( (p=c[i].data.indexOf(str)) > -1) {
                    c[i].splitText(p);
                    c[i].nextSibling.deleteData(0,str.length);
                    c[i].parentNode.insertBefore(
                        document.createElement('b'),c[i].nextSibling
                    ).appendChild(document.createTextNode(str));
                }
            }
        }
    }
    

    That should do it. To use, just call highlight("message"), with whatever text you want. Note that this will be case-sensitive – if you need caseless matching, let me know and I’ll edit to take that into account (although for the most part you could probably get away with highlight("message"); highlight("Message");)

    Also, you can limit the search to a particular element. Let’s say you have <div id="replace-me">, you can limit the search to that element like so:

    highlight("message",document.getElementById('replace-me'));
    

    (You can use any way to get the node, this is just the easiest)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm absolutely new to JavaScript and would like to modify a textarea of a
I'm still new to Javascript and would like to create a 'pages' feature for
I am new to Javascript and object-oriented programming in general. I would like to
I would like to define the following in Javascript: var tileXml = new Array();
I'm completely new to the javascript and ajax world but trying to learn. Right
I'm relatively new javascript, but I am comfortable in other languages. I'm trying to
New to javascript and such, trying to create a loop for fading logos using
I am a javascript novice trying to work with SoundCloud's new HTML5 widgets. I
I'm fairly new to Javascript so I was hoping for a bit of help.
I'm currently trying to learn Knockout.JS - I'm also relatively new to JavaScript itself.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.