How can I simulate an anchor click via jquery?
Try to avoid inlining your jQuery calls like that. Put a script tag at the top of the page to bind to the click
event:
<script type="text/javascript">
$(function(){
$('#thickboxButton').click(function(){
$('#thickboxId').click();
});
});
</script>
<input id="thickboxButton" type="button" value="Click me">
<a id="thickboxId" href="myScript.php" class="thickbox" title="">Link</a>
Edit:
If you're trying to simulate a user physically clicking the link, then I don't believe that is possible. A workaround would be to update the button's click
event to change the window.location
in Javascript:
<script type="text/javascript">
$(function(){
$('#thickboxButton').click(function(){
window.location = $('#thickboxId').attr('href');
});
});
</script>
Edit 2:
Now that I realize that Thickbox is a custom jQuery UI widget, I found the instructions here:
Instructions:
Create a link element (
<a href>
)Give the link a class attribute with a value of thickbox (
class="thickbox"
)In the
href
attribute of the link add the following anchor:#TB_inline
In the
href
attribute after the#TB_inline
add the following query string on to the anchor:?height=300&width=300&inlineId=myOnPageContent
Change the values of height, width, and inlineId in the query accordingly (inlineID is the ID value of the element that contains the content you would like to show in a ThickBox.
Optionally you may add modal=true to the query string (e.g.
#TB_inline?height=155&width=300&inlineId=hiddenModalContent&modal=true
) so that closing a ThickBox will require calling thetb_remove()
function from within the ThickBox. See the hidden modal content example, where you must click yes or no to close the ThickBox.
this approach works on firefox, chrome and IE. hope it helps someone:
var comp = document.getElementById('yourCompId');
try { //in firefox
comp.click();
return;
} catch(ex) {}
try { // in chrome
if(document.createEvent) {
var e = document.createEvent('MouseEvents');
e.initEvent( 'click', true, true );
comp.dispatchEvent(e);
return;
}
} catch(ex) {}
try { // in IE
if(document.createEventObject) {
var evObj = document.createEventObject();
comp.fireEvent("onclick", evObj);
return;
}
} catch(ex) {}
What worked for me was:
$('a.mylink')[0].click()
I've recently found how to trigger mouse click event via jQuery.
<script type="text/javascript">
var a = $('.path > .to > .element > a')[0];
var e = document.createEvent('MouseEvents');
e.initEvent( 'click', true, true );
a.dispatchEvent(e);
</script>