0

I am using an object to store data. How can I get the data for the active person, then when a link is clicked, get it for the person who's link has been clicked?

HTML

<div id="people">
 <a href="#steve" class="active">Steve</a>
 <a href="#mike">Mike</a>
 <a href="#cindy">Cindy</a>
</div>

JavaScript

var $people = $('#people').find('a'),
 activePerson = $people.hasClass('active'),
 people = {
 steve: [{
 color: 'blue',
 pet: 'bird'
 }],
 mike: [{
 color: 'maroon',
 pet: 'dog'
 }],
 cindy: [{
 color: 'pink',
 pet: 'snake'
 }]
 };
console.log(people.activePerson);
$people.click(function (e) {
 e.preventDefault();
 activePerson = $(this).attr('href').slice(1);
 console.log(people.activePerson);
});
asked Aug 4, 2014 at 16:54
1
  • that is not a multi-dimensional array. It is an object with keys representing arrays. Commented Aug 4, 2014 at 16:57

1 Answer 1

2

You can select them in your array like this:

JSFIDDLE: http://jsfiddle.net/L5sHS/

var $people = $('#people').find('a'),
 activePerson = $people.hasClass('active'),
 people = {
 steve: [{
 color: 'blue',
 pet: 'bird'
 }],
 mike: [{
 color: 'maroon',
 pet: 'dog'
 }],
 cindy: [{
 color: 'pink',
 pet: 'snake'
 }]
 };
console.log(people.activePerson);
$people.click(function (e) {
 e.preventDefault();
 activePerson = $(this).attr('href').slice(1);
 console.log(people[activePerson][0].color);
});

In this example I print out the color of the clicked person.

answered Aug 4, 2014 at 17:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.