Note that there are some explanatory texts on larger screens.

plurals
  1. PO$(document).ready and Window onload causing events to fire twice
    text
    copied!<p>I'm trying to fix a bug in my rails 3.2.13 app where whenever I have a document ready or a window onload called, certain click events are fired twice. It only seems to happen after I fire an ajax call. If I stash the commit, then add the code back without the ajax call it works. The only way I can stop it is removing the document ready from the JS file, but I need it for some JQuery to work properly. I think it might be an issue with the asset pipeline but not sure. </p> <pre><code>$(document).ready(function () { $('.first_panel_photos').fadeIn('slow'); var ajax; var user_kits = []; var photo_load_batch_html; var kits_added = 0; $.get('/get_kits', function (data) { for (var i = 0; i &lt; data.length; i++) { photos = data[i].photos; links = []; for (var n = 0; n &lt; photos.length; n++) { link = data[i].photos[n].photo_url; links.push(link); } user_kits.push(new Kit(data[i].kit_name, links)); kit_to_add = $('&lt;button class="btn btn-info added_kit" id=' + (kits_added - 1) + '&gt;Kit&lt;/button&gt;'); $('.cart').append(kit_to_add.fadeIn()); update_total_price(); } }); //Listeners $('.first_panel_photos').on('click', 'img', function (event) { event.stopPropagation(); add_to_kit(this.src); if ($('.kit_photos_row_3').find('img').length === 3 &amp;&amp; $('.buybtn').find('button').length === 0) { $('.buybtn').append($('&lt;button class="btn btn-info add_to_cart"&gt;Add to cart&lt;/button&gt;').fadeIn()); $('.buybtn').append($('&lt;input type="text" class="kit-name" placeholder="name the kit"&gt;').fadeIn()); } }); $('.kit_photos_row_1, .kit_photos_row_2, .kit_photos_row_3').on('click', 'img', function (event) { event.stopPropagation(); event.preventDefault(); remove_from_kit(this, function (img_row) { if ($(img_row).hasClass('kit_photos_row_1')) { shift_photo_from_class('.kit_photos_row_2', function () { shift_photo_from_class('.kit_photos_row_3'); }); } else if ($(img_row).hasClass('kit_photos_row_2')) { shift_photo_from_class('.kit_photos_row_3'); } }); if ($('.add_to_cart').length !== 0) { $('.add_to_cart').remove(); $('.kit-name').remove(); } }); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload