0

I'm just curious why this doesn't work. If I pass the name 'book1' directly it works, but if I pass it through the function's parameter it doesn't.

Any help would be appreciated. Thanks.

var add_to_page = function(item) {
    $('#item .name').text(item.name)
    //$('#book1 .name').text(book1.name) //<--- this works!!

};

add_to_page('book1');
rrk
  • 15,677
  • 4
  • 29
  • 45
fokusfocus
  • 195
  • 1
  • 3
  • 15
  • `add_to_page(book1);` to pass the object or `$('#item .name').text(item)` if you pass a name – mplungjan Feb 15 '16 at 17:25
  • 2
    Duplicate of many. One of them: http://stackoverflow.com/questions/6084858/javascript-use-variable-as-object-name – mplungjan Feb 15 '16 at 18:01

1 Answers1

0

Supposing book1 is an object, your mistake is that you are passing a string to add_to_page(). Try replacing

add_to_page('book1');

with

add_to_page(book1);

EDIT

The selector is wrong too. It should be obvious that $("#item .name") is not the same as $("#book .name"). What you seem to be trying to achieve cannot be accomplished, because you are trying to use one single argument (item) as a string in one place and as an object in another one.

LostMyGlasses
  • 3,074
  • 20
  • 28