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
$(this).addClass for multiple onclick='myfunction()'

This is my code, buttons are creating dynamically

<button class="same_name" onclick='myfunction()'>button name</button>
<button class="same_name" onclick='myfunction()'>button name</button>
<button class="same_name" onclick='myfunction()'>button name</button>
<button class="same_name" onclick='myfunction()'>button name</button>

and I want to add class to button which I clicked

function myfunction()
{
  $(this).addClass("active");
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you're using jQuery, and you're adding the buttons dynamically, strip out the inline JS, and use event delegation to attach one listener to a parent container to watch for events from your same_name buttons, and then add a class for that clicked button. (I'm using toggleClass in this example.)

$(document).on('click', '.same_name', function () {
  $(this).toggleClass('active');
});
.active { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="same_name">button 1</button>
<button class="same_name">button 2</button>
<button class="same_name">button 3</button>
<button class="same_name">button 4</button>

about 4 years ago · Juan Pablo Isaza Report

0

Andy's solution is the proper one. If you wish to roll with your own solution, just pass the event to the function.

function myfunction(event){
  let eTar = event.target
  $(eTar).addClass("active");
}
.active { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="same_name" onclick='myfunction(event)'>button name</button>
<button class="same_name" onclick='myfunction(event)'>button name</button>
<button class="same_name" onclick='myfunction(event)'>button name</button>
<button class="same_name" onclick='myfunction(event)'>button name</button>
 

about 4 years ago · Juan Pablo Isaza Report

0

Here's the vanilla JS answer (jQuery isn't necessary at all, for almost nothing in 2022):

document.addEventListener('click', function ({target}) {
  if (target.matches('button.same_name')) target.classList.add('active');
});
.active { color: red; }
<button class="same_name">button 1</button>
<button class="same_name">button 2</button>
<button class="same_name">button 3</button>
<button class="same_name">button 4</button>

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!