Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
pass parameter to click event of a class

I have this code

<div style="float:left">
  <div class="d-inline ">
    <i id="reply@{@item.Id}" class="bi bi-reply icon-cursor test-comment" onclick="CommentReply(@item.Id)"></i>
  </div>                            
</div>

and js

 function CommentReply(id)
 {
    //to do something
 }

but I want to change to

  $(".test-comment").click(function () {
    //to do something

    // I need Id in here
})

how can I pass the Id parameter to test-comment click event?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try with data attributes: test code here

HTML

<div style="float:left">
  <div class="d-inline ">
    <i data-id="10" data-xx="other x" class="bi bi-reply icon-cursor test-comment" >some text</i> 
  </div>
</div>

Remove onclick= ... and use data-* for your info.

JS

$(".test-comment").click(function () {
    alert($(this).data("id"));
    alert($(this).data("xx"));
})

snippet <3

$(".test-comment").click(function () {
    alert($(this).data("id"));
    alert($(this).data("xx"));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="float:left">
  <div class="d-inline ">
    <i data-id="10" data-xx="other x" class="bi bi-reply icon-cursor test-comment" >CLICK ME!</i> 
  </div>
</div>

about 4 years ago · Santiago Trujillo Relatório

0

With this in plain JS, added helloo just for testing

function CommentReply(el){
  //  `el` here is `this` passed from `onclick` is the reference to the element
  console.log(el.id);
}
<div style="float:left">
  <div class="d-inline ">
    <i id="reply@{@item.Id}" class="bi bi-reply icon-cursor test-comment" onclick="CommentReply(this)">helloo</i>
  </div>                            
</div>

click on helloo and see console for id

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda