Reputation: 1070
Code Edited Please Reaview. i'm trying to show fancybox after testing a value using ajax after clicking a link but the fancybox keeps disappearing and showing up for ever as it's inside a loop any solution ?
$(document).ready(function () {
$("a.add_to_cart[pro_id]").click(function (e) {
e.preventDefault();
var pro_name = $(this).attr("pro_name");
var pro_id = $(this).attr("pro_id");
var qst = "?pro=" + pro_id;
var ajax = false;
ajax = new XMLHttpRequest();
ajax.open("get", "ajax/check_exists.php" + qst);
ajax.onreadystatechange = function () {
if (ajax.readyState == 4 && ajax.status == 200) {
var result = ajax.responseText;
if (result == 'product in cart') {
alert(result);
} else {
$("a.add_to_cart[pro_id=" + pro_id + "]").fancybox({
'transitionIn': 'elastic',
'transitionOut': 'elastic'
}).trigger('click');
}
}
}
ajax.send(null);
});
});
Upvotes: 2
Views: 491
Reputation: 5084
You'll need to unbind the event in order to prevent recursive click triggering:
$("a.add_to_cart").unbind("click");
$("a.add_to_cart").fancybox({
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
}).click();
But you should try to separate the elements because unbind
will destroy the binding for all the elements with your add_to_cart
class.
Try to set id
instead of class
for the elements and as selector, so that only the current clicked element's binding will be destroyed.
Upvotes: 1
Reputation: 476
Just add the click right after you apply the fancybox:
$("a.add_to_cart").fancybox({
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
}).click();
Upvotes: 1