How to add value to an array on click event

Viewed 9294

What I am trying to achieve is to read value on click event and save it to an array. My code:

<a href='#' class="hotel" data-hotel-name='Holiday Inn'>Add to favourites</a>
var hotelName = [];    
$('.hotel').on('click', function(e){
    e.preventDefault();    
    hotelName.push( $(this).data('hotel-name') );
});    
console.log(hotelName.length);

The output is always 0. Could someone tell me what am I doing wrong, please?

1 Answers
Related