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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:08:37+00:00 2026-05-30T16:08:37+00:00

My regular javascript code seems to be outputting stuff to the page either incorrectly

  • 0

My regular javascript code seems to be outputting stuff to the page either incorrectly or in the wrong order, which is weird because the code is very simple (maybe 20 lines tops). My code is below. Note:

  • pttr_data.length will return 150
  • clean_array.length will return 4.
  • output is a div object, i.e. var output = document.getElementById(“results”);

I want my code to output like this:

<div>matcha matchb matchc matchn </div>

But instead it returns:

<div/>matcha matchb matchc match

Here is my code, in which I placed random characters to figure out where stuff is going (you’ll see the ;;;, |||, [[[, }}} in there surrounding the divs):

var len = pttr_data.length;
for (var i = 0; i < len; i++) {
    var clean_array = pttr_data[i].match(RegExp(rexp.value, flags.value));
    output.innerHTML += ";;;<div>|||";
    var lengthy = clean_array.length;
    for (var j = 1; j < lengthy; j++) {
        if( clean_array[j] ) { output.innerHTML += clean_array[j] + ' '; }
        else { output.innerHTML += 'NULL '; }
    }
    out.innerHTML += "[[[</div>}}}\n";
}

This code returns:

;;;<div>|||</div>matcha matchb matchc [[[}}}

Can someone explain why this occurs? Do javascript for loops operate independently (and thus finish at varying speeds), even if they are nested? This makes no sense though, why would someone design a scripting language like that?
How could my code output <div/>blah instead of <div>blah</div> ?

Thanks

  • 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-30T16:08:38+00:00Added an answer on May 30, 2026 at 4:08 pm

    The .innerHTML property is a bit more complicated than just a string you can append to. If you append "<div>" to it (or ";;;<div>|||" in your example) the browser doesn’t wait around to see if you’ll eventually supply a closing </div> tag, it just creates a div immediately. Then you append more text: "matcha", "matchb", etc. And finally you append a closing </div> that the browser ignores because, again, it wasn’t waiting for it, but it keeps the "[[[}}}".

    Instead of appending to .innerHTML as you go, append to a string variable and then set .innerHTML equal to that string at the end.

    If you want to append all elements of clean_array separated by spaces you don’t even need the for loop, you can just use .join():

    output.innerHTML = "<div>" + clean_array.join(" ") + "</div>";
    // or for all from index 1
    output.innerHTML = "<div>" + clean_array.slice(1).join(" ") + "</div>";
    

    But to keep your nested for loop:

    var len = pttr_data.length,
        outputStr = "";  // use a string variable
    
    for (var i = 0; i < len; i++) {
        var clean_array = pttr_data[i].match(RegExp(rexp.value, flags.value));
        outputStr += ";;;<div>|||";
        var lengthy = clean_array.length;
        for (var j = 1; j < lengthy; j++) {
            // I think ternary conditional is neater than if/else
            outputStr += clean_array[j] ? clean_array[j] + ' ' : 'NULL ';
        }
        outputStr += "[[[</div>}}}\n";
    }
    
    output.innerHTML += outputStr;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following javascript code concatenates the matches found by a regular expression. The regular
I have the following very simple Javascript-compatible regular expression: <script type=text/javascript id=(.+) src=([^]+)> I
I have a php page with the follow javascript code in it: $(#saveButton).click(function() {
I have this regular expression in my Javascript code: /##+[A-z]+##/ When using it on
Is there an easy way to convert jQuery code to regular javascript? I guess
I was experiencing some weird behaviour in some of my javascript code, but only
I need to add markup to some text using JavaScript regular expressions. In Python
I need a regular expression in Javascript, as well as in PHP, for Indian
I am using a regular expression in javascript and want to do server side
I came across a strange behaviour when doing some regular expressions in JavaScript today

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.