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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:21:18+00:00 2026-05-31T09:21:18+00:00

Super newbie at Javascript here. I have a problem with whitespace that I’m hoping

  • 0

Super newbie at Javascript here. I have a problem with whitespace that I’m hoping someone can help me with.

I have a function that looks like this:

function createLinks()  
  {
  var i = 0;
  tbody = document.getElementsByTagName('tbody')[3];
    console.log('test order ID: '+document.getElementsByTagName('tbody')[3].getElementsByTagName('tr')[0].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,''))
  trs = tbody.getElementsByTagName('tr');
console.log('trs.length = '+trs.length);
  for (i=0;i<trs.length;i++)
  {
orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'');
    console.log('order: '+orderId);

hrefReturn = 'https://www.example.com/example.html?view=search&range=all&orderId='+orderId+'+&x=13&y=17';
linkReturn = '<a href='+hrefReturn+'>'+orderId+'</a>';
    console.log(linkReturn);

trs[i].getElementsByTagName('td')[1].innerHTML = linkReturn;

  }
}

I call this function using another function when the page is initially loaded. This works perfectly.

However, I also call this function in another way when data on the page changes. There’s a dropdown list that I have an onClick attribute attached to. That onClick event calls the first function, which in turn calls the second function (above). Both of these functions are saved into text variables and appended to the document, as below:

var scriptTextLinks = " function createLinksText()  {  var i = 0;  tbody = document.getElementsByTagName('tbody')[3];   console.log('test order ID: '+document.getElementsByTagName('tbody')[3].getElementsByTagName('tr')[0].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,''));  trs = tbody.getElementsByTagName('tr'); console.log('trs.length = '+trs.length);  for (i=0;i<trs.length;i++)      { orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'').replace(/\s/g,''); orderId = orderId.replace(/\s/g,''); console.log('order: '+orderId); hrefReturn = 'https://www.example.com/example.html?view=search&range=all&orderId='+orderId+'+&x=13&y=17'; linkReturn = '<a href='+hrefReturn+'>'+orderId+'</a>';        console.log(linkReturn);    trs[i].getElementsByTagName('td')[1].innerHTML = linkReturn;   }  console.log('complete'); } "

Finally, here is the specific problem. When THIS version of the same function is called by events on the webpage, it fails to properly delete the whitespace, which breaks the link that it’s supposed to create.

This is the exact problem section of code:

orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'').replace(/\s/g,''); orderId = orderId.replace(/\s/g,''); console.log('order: '+orderId);

So instead of storing the variable like it should, like this:

"XXXXXXXXX"

It is stored like this:

"   
      XXXXXXXXXX
                  "

Which, again, kills the link.

Can anybody clarify what’s going on here, and how I can fix it? Thanks in advance!

  • 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-05-31T09:21:20+00:00Added an answer on May 31, 2026 at 9:21 am

    To strip all that surrounding whitespace you can use the standard .trim() method.

    var myString = "                \n               XXXXXXX           \n         ";
    myString = myString.trim();
    

    You can strip all leading and trailing, and compress internal whitespace to a single space, as is normally done in HTML rendering, like this…

    var myString = "    \n    XXXX  \n     YYYY    \n   ";
    myString = myString.replace(/\s+/g, " ").trim();
    

    Also, see tharrison’s comment below.

    (though my /\s+/g pattern worked fine with the embedded \n newlines)


    Cure for IE<9

    “shim.js”

    (function() {
        if (! String.prototype.trim) {
            //alert("No 'trim()' method. Adding it.");
            String.prototype.trim = function() {
                return this.replace(/^\s+|\s+$/mg, '');
            };
        }
    })();
    

    (Or, if you might want to do other things in your shim…)

    var shim = {
        init: function() {
            if (! String.prototype.trim) {
                String.prototype.trim = function() {
                    return this.replace(/^\s+|\s+$/mg, '');
                };
            }
        }
    };
    shim.init();
    

    Your HTML file

    <script type="text/javascript" src="shim.js"></script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am newbie with Cocoa Touch, I have a problem that I try to
Super-newbie question! I've been looking for a list of all the classes that come
Everyone, I am a newbie to android development. Now I have a question that
Hey, super newbie question. Consider the following WCF function: [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode
I am still newbie to android, please could someone help. I want to use
I'm realizing what a newbie I still am with this problem I have. I
Still somewhat of a newbie... I have an NSMutableArray that stores filenames - when
When using Python's super() to do method chaining, you have to explicitly specify your
I have a super pom defined in which I specify a "resources" directory for
I'm developing on super fast fibre optic connection. I want a tool that allows

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.