3

If I have an object like this:

var item = {
    id: '1',
    description: 'something'
}

then how do I extract one of these values? If I do

alert($(item.id));

then I just get an alert saying 'Object object'. I had a look at this post: Getting the base element from a jQuery object and tried to invoke

alert($(item).get(0)); 

But with the same result.

Also, from a terminology point of view what are objects like this called so that next time I can be a bit more specific?

8
  • 3
    Why not use just alert(item.id);? Commented Apr 24, 2012 at 8:41
  • 4
    What...? Why can't you just call it using its name and key..? jQuery is not a king of javascript, you shouldn't need thy king everytime.. Commented Apr 24, 2012 at 8:42
  • 1
    What are you really trying to do? Commented Apr 24, 2012 at 8:44
  • unfortunately I'm one of those people who started learning about jQuery before javascript. Commented Apr 24, 2012 at 8:45
  • @NiftyDude, not everyone is a JavaScript expert or knows the difference between jQuery and JS. I think your comment could have been a little nicer Commented Apr 24, 2012 at 8:45

4 Answers 4

5

Just use:

alert(item.id);

This: $(item.id) wraps item.id into a jQuery object, that's why you get 'Object object'

PS

What you're creating is an object in object literal notation, that is a set of comma-separated name/value pairs inside curly braces.

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

Comments

0
console.log(item.id)

$() returns a jQuery object.

Comments

0

Try this out:

alert($item .prop('id'));

Comments

0

Using normal procedure:

alert(item.id);

Using jQuery :

alert($(item).get(0).id);

NOTE

 $(item) -> return an array

 $(item).get(0) -> return object which is equal to (var item)

 $(item).get(0).id -> return id value

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.