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

229
Views
How jquery can trigger click event with additional parameter?

In jquery you can easily do this using something like:

//to trigger click with params
jQuery(domElement).trigger( 'click', [ additionalParameters ] );

//to listen click the params
jQuery( domElement ).on( 'click', function( event, parameters ) { 
    event.stopImmediatePropagation();
}

How to do this using pure javascript?

We can't use 'new CustomEvent' because it doesnt accept 'click' as event. We cant use 'new MouseEvent' because unlike 'CustomEvent' it doesnt have ability to pass parameters.

We can use something like this:

//to trigger click with params
domElement.addEventListener("click", function(e) {
    var event = new CustomEvent("fakeClick", {'detail': {
        custom_info: 10,
        custom_property: 20
    }});
    this.dispatchEvent(event);
});

//to listen click the params
domElement.addEventListener('fakeClick', function(event) {
    var parameters = event.detail;
    event.stopImmediatePropagation();
}); 

But we cant use 'stopImmediatePropagation' because it will only affect our 'fakeClick' event. As we want it to affect 'click'.

How jQuery can do this? There should be a way to do this using pure javascript.

Edit: some random guy mentioning about other thread, which is unrelated. I'm asking about how jquery do this. Here's what he mentioned: => How to pass arguments to addEventListener listener function?

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

0

On your fakeClick event, on additionnal parameter send original event too and apply stopImmediatePropagation on it :

//to trigger click with params
domElement.addEventListener("click", function(e) {
    var event = new CustomEvent("fakeClick", {'detail': {
        custom_info: 10,
        custom_property: 20,
        originalEvent: e
    }});
    this.dispatchEvent(event);
});

//to listen click the params
domElement.addEventListener('fakeClick', function(event) {
    var parameters = event.detail;
    var clickEvent = parameters.originalEvent;
    event.stopImmediatePropagation();
    clickEvent.stopImmediatePropagation();
}); 
about 4 years ago · Juan Pablo Isaza Report

0

Hello please check below code.

Your function

function testFunction(num)
{
    $('.my-output').html('Value'+num);
}

Add event to parametner

<div class="my-object">Click Here</div>
<div class="my-output"></div>

Execute Event

$('.my-object').trigger(testFunction(123));

function testFunction(num)
{
  $('.my-output').html('Value'+num);
}
$('.my-object').trigger(testFunction(123));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="my-object">Click Here</div>
<div class="my-output"></div>

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!