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 7989123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:39:32+00:00 2026-06-04T12:39:32+00:00

I am writing a Greasemonkey script in which in one place I’m converting all

  • 0

I am writing a Greasemonkey script in which in one place I’m converting all the spaces in the text into <br> so that the string appears more vertical in its cell in the table.

The HTML that I’m matching is:

<span>
    <img class="icon itemicon" alt="Manual item" ...etc...>
    Chapter 2 reading
    <a title="Sort in descending order" href="index.php....."> etc </a>
</span>

Here is my code:

var child = spanelm.firstChild;
var textChild = child.nextSibling;
textChild.textContent = textChild.textContent.replace(/ /g, "<br>");

My Greasemonkey script is finding the right element and doing the change successfully, but on the web page I see the <br> tags literally:

Chapter<br>2<br>reading

(In another place in my code, I’m doing a similar thing, which seems to work:

spanelm.firstChild.innerHTML = spanelm.firstChild.text.replace(/ /g, "<br>");

but I cannot get that to work in this case, with this organization of the HTML.)

I feel like I need to tell Greasemonkey to eval() the HTML or something…

  • 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-04T12:39:34+00:00Added an answer on June 4, 2026 at 12:39 pm

    A text node doesn’t have an innerHTML, so you can’t insert new tags that way. (You should be very careful about this kind of string-replace of any node using innerHTML — it’s only a matter of time before the code trashes nested elements, inline JS, attached event handlers, etc.)

    To insert tags in a text element, you need to break up that element into new text elements, interspersed with the tags you desire. In this case, replacing each run of spaces with a <br> would use code like this:

    (See it in action at jsFiddle.)

    var precedingNode   = document.querySelector ("span > img.icon.itemicon");
    var node            = precedingNode.nextSibling;
    var words           = node.textContent.split (/ +/);
    var parent          = node.parentNode;
    
    for (var J = 0, numWords = words.length;  J < numWords;  ++J) {
        var newWord     = document.createTextNode (words[J]);
        var newBreak    = document.createElement ("br");
        parent.insertBefore (newWord,  node);
        parent.insertBefore (newBreak, node);
    }
    parent.removeChild (node);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a fairly basic GreaseMonkey script that locates text in a specific
I'm writing a greasemonkey script that creates an auto-complete search type box which makes
Background : I'm currently writing a greasemonkey script that embeds/modifies a specific page's html.
I am writing a simple GreaseMonkey script that has a jQuery plugin called hoverIntent
I'm writing a simple Greasemonkey script to strip out all the images, headings and
I am writing a greasemonkey script that is parsing a page with the following
I'm writing some javascript (a greasemonkey/userscript) that will insert some input fields into a
I'm writing a GreaseMonkey script, as part of which I'd like to sort a
I'm writing a greasemonkey script to make it so that when a user clicks
I'm writing a Greasemonkey script that has a fair few user settings (just using

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.