Autoclose Fancybox when message send

Hello guys,

I found tedious to close the message fancybox everytime, once it displays “Your message has been sent”, you have to click the close “x” in the top right corner, over and over.

So I edited the code from \hivepress\assets\js\common.js, and added the following line :

//below line 1039 : 
window.setTimeout(function(){jQuery(".fancybox-close-small").trigger('click')}, 1500 );

Then all you need to do in minify the JS, as it is the one being used (I used https://minify-js.com/ for that), and replace the original common.min.js* file with my file.

*Do a backup, just in case. :slight_smile:

Of course, I am aware that at the next update this code might be overwritten.
If you have a better solution, let me know.

1 Like

Hi,

Thank you for sharing this solution, it will be useful for our community.

Please note that you can also add a JS snippet through the Code Snippet plugin so that it is not removed in future updates.

I am interested in the JS snippet, if you know about it.
That would definitely be better.

Something like unregistering the original script and replace it by my own ?

No need to replace the core JS, you can try using a custom JS (loaded via a child theme or Code Snippets plugin) and add an event for the form submissions with a delay, then you can use this code to close any open modals:

$.fancybox.close();

Hope this helps

1 Like

Hello @ihor !

Thanks but it’s not working at least not the way I tried to implement it.

Anyway, you put me on the right track : I did not need to edit HivePress’ code!

I shared the improved version for the community :

//auto close message fancybox 1.5 s after submission
jQuery(document).ready(function($) {	
	$(".hp-form--message-send").submit(function(e){
		console.log("HivePress rocks"); 
		//$.fancybox.delay(1500).close(); //not working
		//window.setTimeout(function(){$.fancybox.close();}, 1500 ); //not working	
		window.setTimeout(function(){
            $(".fancybox-close-small").trigger('click')} //awkward, but works. 
        , 1500 ); 
	});
});

Thanks again.

1 Like

Small update : if you want to add this on all popups, (and not just the message one), use this code instead :

	//autoclose message fancy on submit after 2 sec
	$(".hp-form--message-send, .hp-form--offer-make, .hp-form--review-submit, .hp-form--listing-report").submit(function(e){
		window.setTimeout(function(){jQuery(".fancybox-close-small").trigger('click')}, 2000 );
	});

2 Likes