28

How do I make jquery click <a href="test.zip" id="mylink">test</a>

<script>
    // this wont work
    $('#mylink').trigger('click'); 
</script>

Please can you help

3
  • 2
    what do you expect to happen here Commented Jun 14, 2013 at 9:13
  • 1
    the download must begin automatically - thanks Commented Jun 14, 2013 at 9:15
  • 1
    what do you expect will happen? The code you have is for firing of an event. It's not simulating the user clicking the link
    – Rune FS
    Commented Jun 14, 2013 at 9:16

10 Answers 10

66

If your intention is to navigate to the specified URL as if the user had clicked the link try calling the DOM .click() method instead of the jQuery .click() method:

$('#mylink')[0].click();

The jQuery .click() will call event handlers that you've bound but not cause the default click behaviour.

0
13

You need to trigger the default click method, not the one by jQuery. This can be done by adding the default click option within a click event of jQuery using this.

<a href="http://about.com/"></a>

This is how the JavaScript looks. It basically creates the event when the DOM is ready, and clicks it intermediately, thus following the link.

$(function() {
    $('a').click(function() {
        // 'this' is not a jQuery object, so it will use
        // the default click() function
        this.click();
    }).click();
});

To see a live example (opening about.com), see: http://jsfiddle.net/8H9UX/

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

You need to wait until the DOM has finished loading. This can be done with jQuery. The anonymous function is run at page load once all the elements are available in the DOM.

<script>
    $(function() {
        $('#mylink').trigger('click');
    });
</script>
1
  • Yes - at page load an event is emitted informing that the DOM is now available. jQuery listens to this event and fires any bound listeners as per my example.
    – leepowell
    Commented Jun 14, 2013 at 9:31
3

Just click:

$("#mylink").click();

If your scripts are in the head then you need to ensure that the element exists, so the script should be executed when document is ready.

$(document).ready(function () {
    $("#mylink").click();
});
3

try this

setTimeout(() => {
$("#mylink")[0].click();
}, 500);
2

If you are expecting the file to get downloaded, it will not happen becauer trigger() will not trigger the default event.

1
document.getElementById('mylink').click();

trigger('click') will fire the click event but not the default one.

$('a').click(function(){ alert('triggered') }) // this will be fired by trigger
1

use the following way.... since you want to download the file prevent the link from navigating.

$(document).ready(function() {
    $('#mylink').click(function(e) {
       e.preventDefault();  //stop the browser navigating
       window.location.href = 'test.zip';
    });
}); 
1
  • OP is trying to progammatically force a click, not respond to a click from the user.
    – nnnnnn
    Commented Jun 14, 2013 at 9:20
-1

your above code will not work because you have assigned value in href and then you want some operation onclick of this anchor tag.

<a href="test.zip" id="mylink">test</a>

first of all you have assigned .zip to href. So it will open zip file onclick first and will not trigger any other operation in onclick trigger.

so whatever operation you want to do , perform it first then open .zip

use code like below

<a href="javascript:void(0);" id="mylink">test</a>

$('#mylink').click(function(){
    // do your operation here
    // now open zip 
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.