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

148
Views
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 answers
Answer question

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 Report

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 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!