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

175
Views
Click on multiple elements with same ID

My JSP generates two buttons with same id, but when I click on it I want just the one I click on to change style. This is my JSPcode:

<%for(MetodoPagamentoBean m : result){%>    
  <button class="method-1" id="metodo-pagamento-scelto">
    <span><ion-icon name="card"></ion-icon><ion-icon class="checkmark" name="checkmark-circle" id="carta-scelta" style="display:none;"></ion-icon></span>
    <span><b>Numero:</b> <%= "XXXX XXXX XXXX " + m.getNumeroCarta().substring(m.getNumeroCarta().length()-4) %></span>
    <span><b>Intestatario:</b> <%= m.getNomeIntestatario()%></span>
  </button>
 <%} %>

while my jQuery is:

$("#metodo-pagamento-scelto").click(function(){
  $("#carta-scelta").show();
  $("#metodo-pagamento-scelto").css("border", "2px solid green");
});

How can I correct this behavior?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • Fix your JSP. Creating multiple elements with the same ID is clear sign of poor programming and markup habits. Use classes instead: class="metodo-pagamento-scelto"
  • Don't use .css() when not necessary. Use jQuery's .toggleClass() or addClass() instead.
  • Use this in jQuery like $(this) to refer to the event currentTarget Element
$(".metodo-pagamento-scelto")     // Use classes
  .on("click", function() {       // Use the .on() Method
    $("#carta-scelta").show();
    $(this).addClass("active");   // Use $(this) to refer to the clicked button
  });

CSS:

.metodo-pagamento-scelto.active {
  border: 2px solid green;
}
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!