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

114
Views
Why is my jquery executing on second click only?

I've written a jQuery function which updates a div upon clicking another div. However, it is only firing on the second click each time and I'm rather confused.

How can I fix this?

$(document).ready(function(){ 
  $(".block-swatch").click(function(){
    $("#refresh-feefo").load(window.location.href + " #refresh-feefo" );
  }); 
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should consider using .on as it allow to bind event from a parent and if your DOM element #refresh-feefo isn't ready, it will still work.

$(document).ready(function(){ 
  $("body").on('click', '.block-swatch', () => {
    $("#refresh-feefo").load(window.location.href + " #refresh-feefo" );
  }); 
});

I used body , but be careful:

Hierarchical selectors can often be avoided simply by attaching the handler to a more appropriate point in the document. For example, instead of $( "body" ).on( "click", "#commentForm .addNew", addComment ) use $( "#commentForm" ).on( "click", ".addNew", addComment ).

Read Event performance section here for more: https://api.jquery.com/on/

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!