Josef Lundström
Josef Lundström

Reputation: 197

Change jQuery popup to appear on page load

I am trying to create a jQuery popup that will appear automatically when a webpage loads. Here is a working code for a popup which opens up with click on a button that I am using:

$('[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();
});

How do I write the code if I want the popup above to appear as specified? In this format if it's possible please:

$('[data-popup-close]').on('page-load', function(e)  {

Upvotes: 1

Views: 111

Answers (1)

Scott Marcus
Scott Marcus

Reputation: 65808

If you want the popup as soon as the page is ready, you simply pass the function to be executed to the JQuery object. There is no need for binding with on. Keep in mind that now that the function is invoked by a different object, this will be bound to window and not the data-popup-close element.

$(function(e)  {
    var targeted_popup_class = $('[data-popup-close]').attr('data-popup-close');
    $('[data-popup="' + targeted_popup_class + '"]').fadeOut(350);
});

Upvotes: 1

Related Questions