Home > Software design >  I want to convert my Javascript to jQuery and work with Class name
I want to convert my Javascript to jQuery and work with Class name

Time:09-19

I have a autoclick script which is made with javascript codes. It is working only with ID. Not working with Class. I want to convert it to jQuery and working with class. Please help me..

<a href="https://stackoverflow.com" id="autoclick">Autoclick</a>

<script type="text/javascript">
var autoclickBtn = document.getElementById("autoclick");
autoclickBtn.addEventListener("click", () => {
  console.log("Button clicked");
});
var interval = window.setInterval(() => {
  autoclickBtn.click();
}, 2000);
</script>

CodePudding user response:

You just need a jQuery selector and instead of addEventListener(), you may use either click() or on().

$('.autoclick').on("click", () => {
  console.log("Button clicked");
});
window.setInterval(() => {
  $('.autoclick')[0].click();
}, 2000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://stackoverflow.com" >Autoclick</a>

  • Related