0

This is my first post on stackoverflow, so I apologize if my question isn't clear, or this has been answered before. I did try to look, but I think my issue is that I don't know what I'm doing wrong.

I currently have an object, like so:

var character = { name1: { info1: xx, info2: xx, ID: xx}, name2:  { info1: 
xx, info2: xx, ID: xx} }

I have an image that someone can click on, and that image has IDs that match the object IDs.

I'm trying to transfer the click attribute(ID) to a variable, parse it, then use it to access the more complete id information...like so:

var clickedOnId = $(this).attr("id");

function characterID() {
    var parseID = JSON.parse(clickedOnID)
    var accessID = character.name.parseID
    $(".div").html("<h2>" + accessID + "</h2>")
}

I know the click is logging the ID (I've console.log'ed it), so I know the click is receiving the ID attribute information from the image...so the problem has to do with how i'm accessing the object. I've tried not parsing it too, but that doesn't work. Does anyone have any suggestions for what I could do?

Thank you!

1
  • is that it has something to do with character.name in json you actually have name1, name2 etc. Commented Jun 16, 2017 at 17:45

2 Answers 2

2

JSON.parse() is not relevant here. Just a simple test to see if the clicked id matches the object id.

var character = { 
   name1: { 
     info1: "x info", 
     info2: "more x info", 
     ID: "xx"
   }, 
   name2: {
     info1: "y data", 
     info2: "specific y info", 
     ID: "yy"
   }
};


$("div").on("click", function(){

  // Loop over objects
  for(var prop in character){

    // Test to see if we have an id match
    if(character[prop].ID === this.id){
        
        // Begin preparing output
        var ul = document.createElement("ul");
        
        // Loop over sub-object properties and produce output
        for(var subProp in character[prop]){
          var li = document.createElement("li");
          li.textContent = prop + "." + subProp + " = "  + character[prop][subProp]; 
          ul.appendChild(li);
        }
    
        $(".div").html(ul);

    }
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="yy">Try Me</div>
<div id="xx">Try Me</div>
<div class="div"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to up vote and mark as the answer. Good luck.
0

Its not very clear what you are trying to achive.

function characterID() {
    var accessID = character["name" + clickedOnID];
    $(".div").html("<h2>" + accessID + "</h2>")
}

But that will probaly give you "<h2>[object Object]</h2>". So you probably want $(".div").html("<h2>" + accessID.ID + "</h2>") or some other property.

Also remember that clicking on a image is not a very good idea because of accessibility.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.