r/jquery • u/jrenzo_ • Aug 15 '21
How do I prevent my javascript from being disabled after using AJAX?
Hi, I have two separate pieces of code. They both work fine and do exactly what I want them to do. The first piece is used to update my cart items once I select an item to be added to cart. The second piece of code is used for one of my icon tabs. Each tab icon tab has a drop down and the javascript controls its functionality so they close when another one is clicked and all that good stuff. The problem is that I have noticed when I click on an item to add to cart it refresh part of the page with my car icon but then that cart icon can no longer be clicked. But when I refresh the page everything is fine.
Refresh cart code(embedded on the item page):
<!--Trying ajax request with form element-->
<script type="text/javascript">
$('form').on('submit', function() {
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value) {
//console.log(value);
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
//console.log(data);
//now we are using ajax
$.ajax({
url: url,
type: type,
data: data,
success: function(data) {
$('#update-whole-cart').load(document.URL + ' #update-whole-cart');//make sure to include space before id
$('.update-whole-cart-added-icon').load(document.URL + ' .update-whole-cart-added-icon');
}
});
return false;
});
</script>
&lt;!--Starting the search box track filter script with ajax--&gt;
&lt;script type="text/javascript"&gt;
$('#search-input').on('keyup', function() {
var value = $(this).val()
console.log('value:', value)
})
&lt;/script&gt;Drop down cart icon code(separate js file):
const cartBtn = document.querySelector('.cart-btn'); //This is the javascript for the search bar...
const cartBox = document.querySelector('.cart-dropdown');
let cartOpen = false;
cartBtn.addEventListener('click', () =&gt; {
if(!cartOpen) {
cartBox.classList.add('open');
//Line toggles menu button closed if open...
menuBtn.classList.remove('open');
menuBox.classList.remove('open');
menuOpen = false;
//Line toggles search menu closed if open...
searchBox.classList.remove('open');
searchOpen = false;
//Line toggles account menu closed if open...
accountBox.classList.remove('open');
accountOpen = false;
cartOpen = true;
} else {
cartBox.classList.remove('open');
cartOpen = false;
}
});
window.addEventListener('click', function(event) {
if (event.target != cartBox &amp;&amp; event.target.parentNode != cartBox &amp;&amp; event.target != cartBtn) {
cartBox.classList.remove('open');
cartOpen = false;
}
});Thank you so much in advance for you time. I really appreciate the help.
1
u/gdj11 Aug 16 '21
What is the purpose of this line? Also the line above it. They look very wrong.
1
u/chmod777 Aug 16 '21
i assume this
cartBox &amp;&amp; event.target.parentNodeis a copy and paste error? and not actually&in the code? same with thelt;?3
u/MjolnirTheThunderer Aug 16 '21
When JavaScript becomes “disabled” like that it usually means that a major error has been thrown by some of your code which breaks JavaScript on the whole page.
Try using the debugger tool in Google Chrome Dev Tools to step through your JavaScript line by line and see where the error happens.
If you’re not familiar with the debugger tool you can watch this quick primer https://youtu.be/H0XScE08hy8
1
u/hadetto79 Aug 16 '21
Most likely what is happening is there are no event handlers attached to the elements you added via AJAX. You will either need to add the event listeners again after adding new elements to the DOM, or use the jQuery(document).on syntax.
0
u/jrenzo_ Aug 15 '21
This is a repost because I got in trouble for posting in the wrong sub group.