I have the following jQuery page on a Rails page:
$(document).on('click','.reportsArrow', function() {
if ( $(this).parent().hasClass('reportCollapsed') ) {
console.log("A");
$(this).parent().removeClass('reportCollapsed');
}else{
$(this).parent().addClass('reportCollapsed');
console.log("B");
}
});
When I click an item with reportsArrowand without reportCollapsed, the log shows
B
A
It is assumed that he performs the part else, and then performs the part if. I want the function to be executed only once per click and to follow only one code. Why is this done twice and how to stop it? I should note that this works correctly in layouts created by a web designer (HTML / CSS / JS only). It seems the problem is with Rails.
EDIT:
We found a working solution:
$('.reportsArrow').click(function() {
$(this).parent().toggleClass('reportCollapsed');
});
source
share