. $ ('A') trigger ('click'); does not work
How to make jquery by pressing <a href="test.zip" id="mylink">test</a>
<script> // this wont work $('#mylink').trigger('click'); </script> Please, help
If you intend to go to the specified URL as if the user clicked the link, try calling the DOM .click() method instead of the jQuery .click() method:
$('#mylink')[0].click(); jQuery .click() will call event handlers that you bound but did not trigger the default click behavior.
You need to run the default click method, not jQuery. This can be done by adding the default click option to the jQuery click event using this .
<a href="http://about.com/"></a> This is what JavaScript looks like. It basically fires an event when the DOM is ready, and clicks it in between, 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/
If you expect the file to be downloaded, this will not happen. becaer trigger () will not fire a default event.
Just click:
$("#mylink").click(); If your scripts are in the head, you need to make sure that this element exists, so the script must be executed when the document is ready.
$(document).ready(function () { $("#mylink").click(); }); use the following method .... since you want to upload a file to prevent the link from moving.
$(document).ready(function() { $('#mylink').click(function(e) { e.preventDefault(); //stop the browser navigating window.location.href = 'test.zip'; }); }); document.getElementById('mylink').click(); trigger('click') will fire a click event, but not by default.
$('a').click(function(){ alert('triggered') }) // this will be fired by trigger You need to wait for the DOM to finish loading. This can be done using jQuery. An anonymous function runs when the page loads, when all the elements are available in the DOM.
<script> $(function() { $('#mylink').trigger('click'); }); </script> your above code will not work because you assigned a value in href , and then you want to do some onclick operation of this anchor tag.
<a href="test.zip" id="mylink">test</a> the first thing you assigned .zip to href. Thus, it will first open the onclick zip file and will not invoke any other operation in the onclick trigger.
so that the whole operation you want to do, first do it, then open .zip
use code as below
<a href="javascript:void(0);" id="mylink">test</a> $('#mylink').click(function(){ // do your operation here // now open zip });