0

I'm trying to make this possible:

Is there a way or an if statement to call this

$('#add-remove-buttons').find('.button').trigger('click');

After it detects that the #add-remove-buttons is present on the webpage?

-Thanks

MGames
  • 1,171
  • 2
  • 13
  • 24

2 Answers2

2

try first check first element exist or not Check if element exists in jQuery after fire the event after exist

 if ($("#add-remove-buttons").length > 0)
 { 
    $("#add-remove-buttons").trigger('click');
 }
Community
  • 1
  • 1
Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26
0

Whe you insert $('#add-remove-buttons').trigger('click'); statement after the click handler on button then if the button exists it will trigger the action. You need not explicitly check to see if it exists

$(function(){
  
  $('#add-remove-buttons').on('click', function(){
    alert('btn clicked');
  })
  $('#add-remove-buttons').trigger('click');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="add-remove-buttons">Click</button>
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400