Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
Equivalent of attaching a listener to buttons on document ready in jQuery instead of vanilla JavaScript?

How do I convert this JavaScript code to jQuery code?

if (document.readyState == 'loading') {
    document.addEventListener('DOMContentLoaded', ready)
} else {
    ready()
}

function ready() {
    var removeCartItemButtons = document.getElementsByClassName('btn-danger')
    for (var i = 0; i < removeCartItemButtons.length; i++) {
        var button = removeCartItemButtons[i]
        button.addEventListener('click', removeCartItem)
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Running something when the document is ready is handled by the $( document ).ready() method.

getElementByClassName can be replaced by a Class Selector (".class")

Your for loop can be removed because with jQuery methods all are called on each returned element.

addEventListener can be replaced by .click().

Then your code is just three lines long:

$(function(){
    $('.btn-danger').click(removeCartItem)
})
about 4 years ago · Juan Pablo Isaza Report

0

jQuery is a library of Javascript, and therefore nothing needs to be converted.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!