Reputation: 13184
I've got event:
$(document).on("click", ".someClass", function(e){ /*stuff*/ }));
Then I'm making $(".someClass").trigger("click");
but nothing happens.
I think that I would need to trigger event on document somehow... Is there any way to trigger event some way as it would be really clicked so delegated events will be fired too?
Also - I've got multiple elements of someClass
items and sometimes I need to trigger it only on single of them.
Upvotes: 0
Views: 685
Reputation: 16368
Something along these lines should work:
var $container = $('.container');
// create a button
var $button = $('<input value="Click me!" class="btn" type="button"/>');
// add button to container
$container.append($button);
// click listener
$container.on('click', '.btn', function() {
alert('Triggered');
});
// Create an event
var event = jQuery.Event('click');
event.target = $container.find('[type=button]')[0];
// Trigger the event on the container
$container.trigger(event);
Upvotes: 1