0

I have a sample

my JS code sample:

$('.owl-next, .owl-prev').on("click", function() {
    alert("1");
});

There is a click event on two buttons, the problem is that these two div's configure consecutively, and the event I need doesn't work.

I need a suggestion how to make the click event on the two buttons work, even if the buttons appear later.

My javascript code uploads too quickly.

DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
Cristi
  • 531
  • 1
  • 8
  • 21

1 Answers1

2

Use document.ready(). It won't set the code unless the site content and it's components are fully loaded.

Note: this solution doesn't work for asynchronous calls, used for dynamical adding elements into DOM. If you want to add your elements dynamically, check following link: Event binding on dynamically created elements?


$(document).ready(function() {
  $('.owl-next, .owl-prev').on("click", function() {
    alert("1");
  });
});
Community
  • 1
  • 1
kind user
  • 40,029
  • 7
  • 67
  • 77