1

I want my code to execute when an element is shown.

$('.test').on('click', function(){
    $('.sub-slider').toggle();
    $('.sub-slider').on('show', function(){
     console.log("hi");
    });
});
mrfr
  • 1,724
  • 2
  • 23
  • 44
  • Please include all relevant code. but might help https://stackoverflow.com/questions/1225102/jquery-event-to-trigger-action-when-a-div-is-made-visible – Carsten Løvbo Andersen Aug 16 '17 at 11:46
  • Possible duplicate of [jQuery event to trigger action when a div is made visible](https://stackoverflow.com/questions/1225102/jquery-event-to-trigger-action-when-a-div-is-made-visible) – Mathiasfc Aug 16 '17 at 11:48

2 Answers2

3

Try this:

var visible = $("#ele"/*select the element*/).is(":visible");
console.log(visible);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ele"></div>

You can delete the div to test it.

fresh mouse
  • 382
  • 1
  • 6
0

Try like this.Wrap the function inside the toggle.And check the element is display property is block then pass your stuff

 $('.test').on('click', function() {
  $('.sub-slider').toggle(function() {
    if ($(this).css('display') == 'block') {
      console.log('showing')
    }
  });
});

Example

 $('.test').on('click', function() {
      $('.sub-slider').toggle(function() {
        if ($(this).css('display') == 'block') {
          console.log('show')
        }
        else{
         console.log('hide')
        }
      });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="test">click</button>
<p class="sub-slider">text</p>
prasanth
  • 22,145
  • 4
  • 29
  • 53