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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:16:52+00:00 2026-05-18T04:16:52+00:00

Using Javascript. I’m trying to loop through an array encoded with JSON. Here is

  • 0

Using Javascript. I’m trying to loop through an array encoded with JSON. Here is a sample of the array:

{"test1":"some info","test2":"more info","test3":"more stuff"}

Inside each loop I am checking to see if a DIV id exists with the name of the keys.

<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>

I’m using a for() loop, but I can’t get it to work. If I remove the for() loop it works just fine if I search for only 1 DIV id.

for(var key in responseText)

Here is the script. Does anyone know how I can loop through the array from responseText using the array keys as the names of the DIV id’s?

<script>
function loadInfo() {
  var req = new Request({
    method: 'get',
    url: 'getinfo.php,
    noCache: true,
    onRequest: function() {
      for (var key in responseText) {
        if (document.getElementById(key)) {
          $(key).set('html', 'Loading');
        }
      }
    },
    onComplete: function(responseText, responseHtml) {
      if (JSON.decode(responseText) != null) {
        var data = JSON.decode(responseText);
        for (var key in responseText) {
          if (document.getElementById(key)) {
            $(key).set('html', data[key]);
          }
        }
      }
    },
    onFailure: function() {
      for (var key in responseText) {
        if (document.getElementById(key)) {
          $(key).set('html', '-');
        }
      }
    }
  }).send();
}
window.addEvent('domready', function() {
  loadInfo();
});    
</script>
  • 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-18T04:16:53+00:00Added an answer on May 18, 2026 at 4:16 am

    You have to decode the JSON before you iterate over the keys. So, where you say:

    for(var key in responseText) {
    

    replace that with:

    for(var key in data) {
    

    assuming

     var data = JSON.decode(responseText);
    

    Also, some of your callback functions don’t specify responseText as a parameter. If you want to access this for each callback, you have to explicitly include responseText as a parameter. Example:

    onRequest: function(){
    

    should be:

    onRequest: function(responseText){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I using JavaScript JSON library to parse JSON encoded array, received via POST. Here
Using Javascript I'm trying to loop through a 3 level nested array with the
Using Javascript I want to have a JSON or array that will store id
Using JavaScript I'm trying to split a paragraph into it's sentences using regular expressions.
Using javascript, how do I add some data to the query string? Basically I
Using javascript I'm trying to load images not on the current page (from an
Using javascript, I am trying to change the selection of the listbox item like
Using Javascript/jQuery, how can I pause (or resume) the following loop when the P
Using javascript, how can I add to an array an element which contains fields
using javascript validation and some jquery to show variable error messages in the DOM,

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.