-15

How do I click this using JavaScript?

The "a" only have href and only id on the "div"

<div id="id">
  <a href="link">
    </a>
  </div>
RobinPH
  • 1
  • 1
  • 1
    Do you have some JS events attached to the clicking of that link? Or you just want to change the window location to a new URL? – Brian Glaz Oct 07 '16 at 15:08
  • 5
    If you google your title, you'll get tons of results. Please utilize your search skills :) – Sterling Archer Oct 07 '16 at 15:08
  • follow this link. its already there as many people says.http://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript – Aniruddha Das Oct 07 '16 at 15:21
  • try to search before asking any question if its already available – Aniruddha Das Oct 07 '16 at 15:23
  • This wants un-marking as a duplicate, because the duplicate link is terrible. It's not using snippets / it never even had answer marked as accepted. All it would do is confuse anyone coming here. My solution worked, and was marked down without a hint of a reason. – Keith Oct 07 '16 at 15:24

2 Answers2

-1

Here I've just made the a-link, do a console.log().

But if the onclick is left off, and the url didn't have a hash it would still be ok. It's just the console.log() is better way of seeing what's happening inside a snippet.

document.querySelectorAll('A')[0].dispatchEvent(
  new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': true
  })
);
<div id="id">
  <a href="#" onclick="console.log('link clicked')">123
  </a>
</div>
Keith
  • 22,005
  • 2
  • 27
  • 44
-7

put javascript: before your function name then it will call your js function.

<div id="id">
  <a href="www.google.com" id="myLink">
  </a>
</div>

in your js code.

$("myLink").click();
user3754008
  • 275
  • 2
  • 12