Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to convert my Javascript to jQuery and work with Class name

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 and working with class and jQuery. 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>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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" class="autoclick">Autoclick</a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading