All Day All Day - 4 months ago 13
jQuery Question

Jquery Open And close with the same button

I have a button that opens a small window to show extra information.
At the moment it only opens and I have to click the close button to close it, I really don't want this close button but I would like to be able to open and close it with the same button.

$(function() {
//----- OPEN
$('[data-popup-open]').on('click', function(e) {
var targeted_popup_class = jQuery(this).attr('data-popup-open');
$('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);

e.preventDefault();
});

//----- CLOSE
$('[data-popup-close]').on('click', function(e) {
var targeted_popup_class = jQuery(this).attr('data-popup-close');
$('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);

e.preventDefault();
});
});


Can anyone help me in this?

Answer

Try fadeToggle() jQuery function.

$('button').on('click', function(e)  {
    var targeted_popup_class = jQuery(this).attr('data-popup-open');
    $('[data-popup="' + targeted_popup_class + '"]').fadeToggle(350);

    e.preventDefault();
});
Comments