JackTheKnife JackTheKnife - 5 months ago 25
Javascript Question

Disable jQuery function on a particular page

I have a jQuery function which displays a lightbox pop-up:

function flatsomeQuickView(element) {
$(element).click(function(e){
$(this).after('<div class="ux-loading dark"></div>');
var product_id = $(this).attr('data-prod');
var data = { action: 'ux_quickview', product: product_id};
$.post(ajaxURL.ajaxurl, data, function(response) {
$.magnificPopup.open({
removalDelay: 300,
items: {
src: '<div class="product-lightbox">'+response+'</div>',
type: 'inline'
}
});
$('.ux-loading').remove();
$('.product-lightbox .product-gallery-slider').flickity({
cellAlign: "center",
wrapAround: true,
autoPlay: false,
prevNextButtons:true,
percentPosition: true,
imagesLoaded: true,
lazyLoad: 1,
pageDots: false,
rightToLeft: false
});
setTimeout(function() {

// Run Variations Form Scripts
if ($('.product-lightbox form').hasClass('variations_form')) {
$('.product-lightbox form.variations_form').wc_variation_form();
}

$(".product-lightbox form.variations_form").on( "show_variation", function (event, variation) {
if(variation.image_src){
$('.product-lightbox .product-gallery-slider .slide.first img').attr('src',variation.image_src);
$('.product-lightbox .product-gallery-slider .slide.first a').attr('href',variation.image_link);
$('.product-lightbox .product-gallery-slider').flickity( 'select', 0);
}
});

// Create QTY Buttons
$('.product-lightbox').addQty();

}, 600);
});

e.preventDefault();
}); // product lightbox
}
flatsomeQuickView('.quick-view');


which I don't want to execute on a home page (Wordpress site). For that I have created a WP action in the functions.php file

function quick_view_override() {

global $myvar;

if($myvar == 'home') {
echo <<<EOT
<script>
function flatsomeQuickView(element) {
$(element).click(false);
}
</script>
EOT;
}
}

add_action( 'wp_footer', 'quick_view_override', 100 );


but somehow
.click()
is not disabled for
flatsomeQuickView
function.

Answer

jQuery's click (a shortcut for .on) does not take a boolean (see docs).

Use $(element).off('click'); if you're OK with disabling all click handlers on that element.

Comments