Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:34:20+00:00 2026-06-13T05:34:20+00:00

I am using this code the create an onshow event: (function($){ $.fn.extend({ onShow: function(callback,

  • 0

I am using this code the create an onshow event:

(function($){
  $.fn.extend({ 
    onShow: function(callback, unbind){
      return this.each(function(){
        var obj = this;
        var bindopt = (unbind==undefined)?true:unbind; 
        if($.isFunction(callback)){
          if($(this).is(':hidden')){
            var checkVis = function(){
              if($(obj).is(':visible')){
                callback.call();
                if(bindopt){
                  $('body').unbind('click keyup keydown', checkVis);
                }
              }                         
            }
            $('body').bind('click keyup keydown', checkVis);
          }
          else{
            callback.call();
          }
        }
      });
    }
  });
})(jQuery);

Then I’m calling it:

$(document).ready(function(){

    $('.mydiv').onShow(function(){
        alert('myDiv is now showing');

    });

});

I’m also toggling the link which triggers the div to show and hide:

$('.myLink').click(function(){
        $(".mydiv").slideToggle();
});

and finally here is the html:

<a href="#" class="myLink">Step 1</a>
        <div class="myDiv">content here</div>

The problem is that the alert is only showing once when I click the link after the page is loaded. If I click it again I won’t show.

Any ideas why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:34:22+00:00Added an answer on June 13, 2026 at 5:34 am

    I think this is a misunderstanding of how events work in javascript. What your custom extension is doing is running once. The line $('body').bind('click keyup keydown', checkVis); listens for the click keyup and keydown events, but this listener is only applied to elements that are hidden and have passed the if($(this).is(':hidden')){ check.

    So if the element in question starts off hidden, it will respond with checkVis on ‘click keyup and keydown’, but that is the only instance in which anything would run after onShow extension finishing running the first time.

    .slideToggle(); does not fire any events by default, so your extension cannot listen for such an event to fire.

    The closest you can get to listening for an element changing its visibility would be to use something like the dom mutation event, but that has be deprecated and is not cross browser supported.

    If you really need the ability to run an extension whenever an element is “shown” by jQuery the best way I can think of would be to extend the jQuery functions you wish to use to fire a custom event, which would looks something like this:

        var existingSlideToggle = $.fn.slideToggle;
    
        $.fn.slideToggle = function(){
             var returner = existingSlideToggle(this, arguments);
             $(this).trigger('customOnShowEvent');
             return returner;
        }
    

    Then you can do this:

        $('#foo').on('customOnShowEvent', function(){  
           // do stuff 
        });
    

    Or to continue your extension strategy:

    (function($){
      $.fn.extend({ 
        onShow: function(callback, unbind){
          return this.each(function(){
            var obj = this;
            var bindopt = (unbind==undefined)?true:unbind; 
    
            var localRun = function(){
              if($(obj).is(':hidden')){
                var checkVis = function(){
                  if($(obj).is(':visible')){
                    callback.call();
                    if(bindopt){
                      $('body').unbind('click keyup keydown', checkVis);
                    }
                  }                         
                }
                $('body').bind('click keyup keydown', checkVis);
              }
              else{
                callback.call();
              }
            }
    
            if($.isFunction(callback)){
                localRun();
                $(obj).on('customOnShowEvent', localRun);
            }
    
          });
        }
      });
    })(jQuery);
    

    This way it will run the first time, and then re-run whenever the new ‘customOnShowEvent’ is fired (“triggered”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this function in my code and I'm using throw to create meaningful
Currently I'm using this code to create a custom filter: var fName = new
I'm saving an uploaded image using this code: using (var fileStream = File.Create(savePath)) {
I'm using this code to create a custom Event class: package evt { import
I created a rectangle by using this code: var drawComponent = Ext.create('Ext.draw.Component', { autoSize:
I'm using this code to create multiple functions wrappers using variadic templates: // Compile
I have create a vcf file that contains contacts by using this code ContentResolver
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
I want to create an item to sitecore using code behind. I found this
i am trying to create slider movement graphics. The code explain better this. using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.