I have a grid of cards on an index page. I want to be able to click on a card link called "Details" to expand that card to a larger size and unhide some additional text. This all works pretty good, except I tend to have to click on the "Details" link 2-3 times for the click event to fire. It seems I have to click on it exactly 3 times for it fire!
This application is built on Ruby on Rails 5.2 and the file is show.js.erb in the view directory.
This is the file:
$(document).ready(function() {
$('.show').bind('ajax:success', function() {
$('.card').click(function(e) {
e.preventDefault();
clickToExpandCards($(this));
});
});
function clickToExpandCards($obj){
let clickedElement = $obj;
if (clickedElement.hasClass('card-expand')) {
clickedElement.find('.card-hidden-text').hide();
clickedElement.removeClass('card-expand');
} else {
clickedElement.find('.card-hidden-text').show();
clickedElement.addClass('card-expand');
}
};
});
This is the erb link I'm clicking on to fire it.
<div class='card-buttons'>
<%= link_to 'Details', cocktail, :class => 'card-link', remote: true, class: 'show' %>
<%= link_to 'Edit', edit_cocktail_path(cocktail), :class => 'card-link' %>
<%= link_to 'Remove', cocktail, :class => 'card-link', method: :delete, data: { confirm: 'Are you sure?', remote: true }, class: 'delete' %>
</div>
As you can see, once it is expanded, a new click on the "Details" link will return it to its normal size. Frustratingly, that often happens with a single click.
I suspect I've implemented this in a way that is slow to process.
What is a better approach?