Your selector is wrong. Simple use:
$("#favoriteartist");
And also you should use .text
not .val
:
favorites.push( $("#favoriteartist").text() );
And if you are using jQuery 1.7+ you should use .on
not .live
:
$(document).on('click', '#favoriteadd', function() {
var favorites = []
favorites.push( $('#favoriteartist').text() );
console.log(favorites);
});
Now you can change document
with a static selector. If #favoriteadd
is in the docuement when the event is bound you can simple bind it as a normal event:
$('#favoriteadd').on('click', function() {
var favorites = []
favorites.push( $('#favoriteartist').text() );
console.log(favorites);
});
h5 #favoriteartist
is like saying:
- Give me all h5 elements on the whole page.
- Now give me all elements with the id
favoriteartist
witch is a (grand-)child of these h5.
We know that an id in html is always unique, so we simple say:
- Give me the element with the id
favoriteartist
.