39386

jquery click() on a link

Question:

I'm trying to trigger a click on a link when a page loads via ajax call, I tried all methods I found, but I can't manage to do that.

Considering that the link on which I want to trigger the click is given by this code

<a href="download.php?file=cloud/index.html" target="_blank" id="cliccami">Click here</a>

Here are the codes that I tried

<script> $("#cliccami").trigger("click"); </script> <script> $("#cliccami")[0].click(); </script> <script> window.open($("#cliccami").attr("href"), "_blank"); </script>

Both script and element are in the page loaded via ajax call. Could anyone help me to find out what's the matter?

Answer1:

EDIT, I think this is what you're looking for:

html:

<a href="http://www.google.com" target="_blank" id="cliccami">Click here</a>

jQuery:

$(document).ready(function() { $('#cliccami').click(function(e) { window.open($(this).attr('href')); e.preventDefault(); }); $('#cliccami').click(); });

In my example (JSFiddle: <a href="http://jsfiddle.net/qw9BW/" rel="nofollow">http://jsfiddle.net/qw9BW/</a>) #cliccami's click event is captured, alerted, then stopped. then a $.click() is ran on page load to force the click event to happen.

Answer2:

Have you tried using a <em>ready</em> function?

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

Recommend

  • jquery/javascript post to new window when pressing a preview button
  • Getting Javascript Errors From Another Window
  • jQuery random blockquote
  • Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often
  • Multilingual set up of codeigniter
  • Is there an HTML code that can make my background picture transparent and my text non-transparent?
  • IOS > Open URL in Safari with POST
  • remove unicode characters but keep all special and English characters with preg_replace
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Meteor helpers not available in Angular template
  • Alert pop up with LWUIT
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • DomPDF {PAGE_NUM} not on first page
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Javascript simulate pressing enter in input box
  • Shallow update not allowed (git > 1.9)
  • Importing jscolor library in angular 2
  • jquery mobile loadPage not working
  • Google cloud sdk not working when python points python3
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Why winpcap requires both .lib and .dll to run?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to get icons for entities from eclipse?
  • A cron job substitute?
  • How to delete a row from a dynamic generate table using jquery?
  • How to include full .NET prerequisite for Wix Burn installer
  • json Serialization in asp
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to stop GridView from loading again when I press back button?
  • Qt: Run a script BEFORE make
  • JaxB to read class hierarchy
  • Conditional In-Line CSS for IE and Others?