javascript - JS not working in Rails -
i familiar turbolinks issue, seems fix not working.
i have this:
var ready = function () { $('.add_to_cart').click(function (e) { e.preventdefault(); var pid = $(this).attr('data-pid'); $.post('/cart/'+ pid, function(data){ var data = json.parse(json.stringify(data)); $('#items-in-cart').text(data['cart_size']); console.log(data); }); }); }; $(document).ready(ready); $(document).on('page:load', ready);
my javascript won't trigger on document load. have refresh. have done fix on other pages, , worked. else should try / for?
edit:
i using jquery (through jquery-rails gem, 3.1.2)
i found error in jquery:
uncaught typeerror: undefined not function ret = ( (jquery.event.special[ handleobj.origtype ] || {}).handle || handleobj.handler ) .apply( matched.elem, args );
found reference 'bug' here.
edit 2:
here html.slim:
=link_to "add cart", "#", class: 'add_to_cart', data: {pid: @product.id}
if rid of line, rest of javascript functions work in ready block on page load. if keep it, uncaught typeerror.
edit 3:
i removed turbolinks app , works fine. i'd keep turbolinks, @ point it's solution.
you need bind click event since content loading via ajax, this
$('body').on('click', '.add_to_cart', function(e){ // js code });
Comments
Post a Comment