Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does preventDefault not work but return false does

Does anyone have any idea why my preventDefault code isn't working? return false returns fine, but it is my understanding that's not really the 'proper' way?

 if ($('.signup').length == 0) {
        $('.star').on('click',function(e){
            e.preventDefault();
            var starElement = $(this);
            var resourceId = starElement.parents('li').data('id');

            updateFavoritesSpan( starElement, starElement.hasClass('starred') );

            starElement.toggleClass('starred');
            starElement.parents('li').toggleClass('fvtd');
        });
        // voting
        $('.voting').on('click .up', function(e){
            e.preventDefault();
            sendVote($(this), 1);
        });

        $('.voting').on('click', '.down', function(e){
            e.preventDefault();
            sendVote($(this), -1);
        });
    }
like image 756
Tallboy Avatar asked Dec 15 '22 22:12

Tallboy


1 Answers

return false; does both preventDefault and stopPropagation.

preventDefault stops the "default" action on the element, stopPropagation stops the event from bubbling up to the parent elements.

My guess is that there is an event on the parent that is still getting triggered when you only do preventDefault.

like image 197
Rocket Hazmat Avatar answered Dec 29 '22 12:12

Rocket Hazmat