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

123
Views
JQuery - Function applying only for first element with class

I'm trying to update a modal & video attributes on button click.

It works well but the problem is that it only apply this when i click on the first button ( first element with the class videoBtn ), i want it to be applied on all other buttons with the same class

here is my code

$(".videoBtn").click(function(){
        $("#video").attr('src',$(".videoBtn").data('src'));
        $(".modal").attr('id', $(".videoBtn").data('target').substring(1));
      });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are not using value of currently clicked element, but first with class, you should use $this instead of $('.videoBtn')

$(".videoBtn").click(function(){
   $("#video").attr('src',$(this).data('src'));
   $(".modal").attr('id', $(this).data('target').substring(1));
});
about 4 years ago · Juan Pablo Isaza Report

0

$(".videoBtn").each(function(){
  $(this).on('click', function() {
    $("#video").attr('src',$(".videoBtn").data('src'));
    $(".modal").attr('id', $(".videoBtn").data('target').substring(1));
  })
})
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!