How to Get Jquery.Trigger('Click'); to Initiate a Mouse Click

How to get JQuery.trigger('click'); to initiate a mouse click

You need to use jQuery('#bar')[0].click(); to simulate a mouse click on the actual DOM element (not the jQuery object), instead of using the .trigger() jQuery method.

Note: DOM Level 2 .click() doesn't work on some elements in Safari. You will need to use a workaround.

http://api.jquery.com/click/

How to trigger a click on a link using jQuery

If you are trying to trigger an event on the anchor, then the code you have will work I recreated your example in jsfiddle with an added eventHandler so you can see that it works:

$(document).on("click", "a", function(){
$(this).text("It works!");
});

$(document).ready(function(){
$("a").trigger("click");
});

Are you trying to cause the user to navigate to a certain point on the webpage by clicking the anchor, or are you trying to trigger events bound to it? Maybe you haven't actually bound the click event successfully to the event?

Also this:

$('#titleee').find('a').trigger('click');

is the equivalent of this:

$('#titleee a').trigger('click');

No need to call find. :)

how to trigger a double click on a single click using jquery

Not sure where exactly your problem is, but this code works exactly as expected:

$("#container").dblclick(function() {   //code executed on jQuery double click rather than mouse double click  alert('dblclick');});$("#container").click(function() {   alert('click');  $(this).dblclick(); });
$("#container2").click(function() { $("#container").click();});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="container">click 1</div><div id="container2">click 2</div>

How to trigger click event on href element

I do not have factual evidence to prove this but I already ran into this issue. It seems that triggering a click() event on an <a> tag doesn't seem to behave the same way you would expect with say, a input button.

The workaround I employed was to set the location.href property on the window which causes the browser to load the request resource like so:

$(document).ready(function()
{
var href = $('.cssbuttongo').attr('href');
window.location.href = href; //causes the browser to refresh and load the requested url
});
});

Edit:

I would make a js fiddle but the nature of the question intermixed with how jsfiddle uses an iframe to render code makes that a no go.

$('a').trigger('click'); not working

$(document).ready(function(){
$('#mylink').trigger('click');
});


Related Topics



Leave a reply



Submit