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

  • Home
  • SEARCH
  • 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 881245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:13:47+00:00 2026-05-15T12:13:47+00:00

i have a script that calls a php file and gets an JSON object

  • 0

i have a script that calls a php file and gets an JSON object from it.
I’m tring to get the ids from the object attached to a specific element that is created by iterating with $.each the json object. Here is what i have done so far:

$(document).ready(function()
{
    $("#div2").hide('slow');
    $("#div1").empty().html('<img src="ajax-loader.gif" />');
    $.ajax(
    {
        type: 'post',
        async: true,
        url: "Parents.php",
        data: {'id' : 12200},
        dataType: "json",
        cache: false,
        success: function(json_data)
        {
            $("#div1").empty();
            $.each(json_data, function(key, value)
            {
                $("#div1").append('<p class="node"><b>['+key+']</b> => '+value+'</p>');
                $(this).data('id', key);
            });
        }
    });
    $("p.node").click(function()
    {
        var id = $(this).data('id');
        alert('The ID is: ' + id);
    });
});

What I’m trying to achieve is making every node aware of it’s ID so when i click, hover etc. i can use that id to do something else like call an other php file with that id.

I’m using fireBug and fireQuery and i noticed that each paragraph that is created has an id and the value of it is the same for all plus it’s the value of the last id (145).

Here is the JSON data i get from the php file (json_encode method):

{"908":"one",
 "143":"two",
 "104":"three",
 "9655":"four",
 "144":"five",
 "142":"six",
 "145":"seven"}

Tnx in advance any idea/help would be appreciated.

  • 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-15T12:13:47+00:00Added an answer on May 15, 2026 at 12:13 pm

    You’re assigning the data to thing json pair you’re looping over…change it up a bit, like this:

    $('<p class="node"><b>['+key+']</b> => '+value+'</p>').data('id', key)
                                                          .appendTo("#div1");
    

    This creates the object using $(html), sets .data() on that, then appends it using .appendTo().


    Additional fix, for anyone finding this later:

    $("p.node").click(function() {
    

    needs to be:

    $("p.node").live('click', function() {
    //or:
    $("#div1").delegate('p.node', 'click', function() {
    

    Since the success function runs later, the $("p.node") won’t find any of the elements created in it, whereas .live() will work, even if they’re created later.

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

Sidebar

Related Questions

I have a html page that calls a php object to get some data
I have a script that calls an application that requires user input, e.g. run
I have a Python script that calls an executable program with various arguments (in
I have a script that retrieves objects from a remote server through an Ajax
I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a script that takes a table name and generates a control file
I have a php script that runs a mysql query, then loops the result,
I have a PHP page that gets its content by making an HTTP request
I have a script that forces a download and I make a call to
I have a Python script I recently wrote that I call using the command

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.