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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:38:25+00:00 2026-06-08T08:38:25+00:00

I am getting in JSON data, I am using dom to display the data,

  • 0

I am getting in JSON data, I am using dom to display the data, I want to embed a link to each search result displayed which when clicked passes that instance of data to another <div> block and display that result.

My HTML code is:

<html>
<body>
<div id:block 1 >
<div id:block 2>
<div id:block 3>
</body>

My JavaScript function is:

function addBooks(data) { // the data is a list of JSON objects


    var listdiv = document.createElement('li');
    listdiv.setAttribute('id', 'gBookListDiv');
    listdiv.innerHTML = ("Books Found:");
    parent.appendChild(listdiv);



    for(i = 0; i < data.length; i++) {


        //create each list item 
        var listItem = document.createElement('li');
        var link = document.createElement('a');
        listItem.setAttribute('id', 'gBookListItem');
        parent.appendChild(listItem);
        link.setAttribute('href', '#');
        link.setAttribute('onclick', 'displayBook(data[i])');

        listItem.appendChild(link);
    }
}

function displayBook(bookData) {
    alert(bookData.title);
    if(document.getElementById("block2").style.display == "block") {
        document.getElementById("block2").style.display = "none";
        document.getElementById("block3").style.display = "block";
    }
    var headerTitle = document.getElementById('book-profile-title-header');
    headerTitle.innerHTML = bookData.title;

    var title = document.getElementById('book-profile-pic');
    headerTitle.innerHTML = bookData.title;

}

For some reason, I am not able to pass the instance of data to the displayBook function.

This is the error I get on Chrome

Uncaught ReferenceError: data is not defined

  • 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-08T08:38:26+00:00Added an answer on June 8, 2026 at 8:38 am

    The problem lies in this code:

    link.setAttribute('onclick', 'displayBook(data[i])');
    

    The handler runs in the global context so data is not defined there. Use a real function instead:

    (function(i) {
        link.onclick = function() { displayBook(data[i]); }
    })(i);
    

    The self-executing function around it is necessary to create a separate i in each iteration.

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

Sidebar

Related Questions

I am using json data received from server to display images. I am getting
I am getting JSON string from website. I have data which looks like this
I am trying to display data that I am getting from server through json.
I'm having trouble getting JSON data from my Rails app to display on a
Is there any standard way of getting JSON data from RESTful service using Python?
I am brand new to actually using an API, getting the JSON data and
Current requirement is on button click I am getting a json data through ajax
I'm having a little dilemma with an iphone project. I'm getting some JSON data
I am populating the list view with data I am getting from a JSON
I m getting Time data in this format in an JSON Object. created_time: 2012-04-01T15:02:52+0000

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.