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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:04:52+00:00 2026-05-22T23:04:52+00:00

A simple question I’m sure, but I can’t figure it out. I have some

  • 0

A simple question I’m sure, but I can’t figure it out.

I have some JSON returned from the server

    while ($Row = mysql_fetch_array($params))
    {
        $jsondata[]= array('adc'=>$Row["adc"], 
                           'adSNU'=>$Row["adSNU"],
                           'adname'=>$Row["adname"],
                           'adcl'=>$Row["adcl"],
                           'adt'=>$Row["adt"]);
    };

echo json_encode(array("Ships" => $jsondata));

…which I use on the client side in an ajax call. It should be noted that the JSON is parsed into a globally declared object so to be available later, and that I’ve assumed that you know that I formated the ajax call properly…

if (ajaxRequest.readyState==4 && ajaxRequest.status==200 || ajaxRequest.status==0)
        {
            WShipsObject = JSON.parse(ajaxRequest.responseText);

            var eeWShips = document.getElementById("eeWShipsContainer");

                for (i=0;i<WShipsObject.Ships.length;i++)
                {
                    newElement = WShipsObject.Ships;
                    newWShip = document.createElement("div");

                    newWShip.id = newElement[i].adSNU;
                    newWShip.class = newElement[i].adc;

                    eeWShips.appendChild(newWShip);


                } // end for

        }// If

You can see for example here that I’ve created HTML DIV elements inside a parent div with each new div having an id and a class. You will note also that I haven’t used all the data returned in the object…

I use JQuery to handle the click on the object, and here is my problem, what I want to use is the id from the element to return another value, say for example adt value from the JSON at the same index. The trouble is that at the click event I no longer know the index because it is way after the element was created. ie I’m no longer in the forloop.

So how do I do this?

Here’s what I tried, but I think I’m up the wrong tree… the .inArray() returns minus 1 in both test cases. Remember the object is globally available…

$(".wShip").click(function(){

    var test1 = $.inArray(this.id, newElement.test);
    var test2 = $.inArray(this.id, WShipsObject);

    //alert(test1+"\n"+test2+"\n"+this.id);

});
  • 1 1 Answer
  • 1 View
  • 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-22T23:04:53+00:00Added an answer on May 22, 2026 at 11:04 pm

    I could not find a way of finding the index as asked, but I created a variation on the answer by Devraj.

    In the solution I created a custom attribute called key into which I stored the index.

    newWShip.key = i;
    

    Later when I need the index back again I can use this.key inside the JQuery .click()method:

    var key = this.key;
    var adt = WShipsObject.Ships[key].adt;
    

    You could argue that in fact I could store all the data into custom attributes, but I would argue that that would be unnecessary duplication of memory.

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

Sidebar

Related Questions

Simple question, but I can't seem to figure it out. I have a list,
simple question maybe but can't find solution. I have button for refresh/update some contents
Simple question but I can't seem to find the answer - can someone tell
Simple question that we've spent to much time on. We have a server side
Simple question. I have a TreePath to a node in my JTree. How can
Simple question but I can't find the answer anywhere on MSDN... Looking for the
Simple question I guess, but a fundamental one and I'm not sure of the
Simple question, but one I can't find an answer to on the MSDN subscriber
Simple question, but for some reason I couldn't find the exact answer on Google:
Simple question. How can I pass a variable from the URL to the view?

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.