Q:

How to add value to an array on click event

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?

A:

Your logic is fine, you just need to read the length of the array after you modify it in the click handler:
var hotelName = [];    
$('.hotel').on('click', function(e){
    e.preventDefault();    
    hotelName.push( $(this).data('hotel-name') );
    console.log(hotelName.length); // < read the length of the amended array here
});

Working example

