3

I try to simulate click on a link with javascript

here is the code:

document.getElementsByClassName('link-a')[2].click();

When i try this

document.getElementsByClassName('link-a')[2];

I have the link that ai want to simulate a click but when i add the click() function it's return undefined and there is no click at all...

can you help me please?

Thank you for your help

Anwar Korti
  • 183
  • 2
  • 3
  • 10
  • Your browser support getElementByClassName ? : https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByClassName – d.danailov Jul 10 '13 at 14:23
  • 1
    With pure javascript i don´t think this works, you have to something mentioned [here](http://stackoverflow.com/questions/2705583/simulate-click-javascript). Or just use jQuery. – luk2302 Jul 10 '13 at 14:23
  • I use the chrome console – Anwar Korti Jul 10 '13 at 14:26
  • 1
    [Your example works fine for me](http://jsfiddle.net/Ta6nH/) – musefan Jul 10 '13 at 14:27
  • Please show an example with some HTML. The `click()` method is supposed to return undefined so that's not out of the ordinary. Also, see: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.click – Eddie Flores Jul 10 '13 at 14:31

1 Answers1

1

so

document.getElementsByClassName('link-a')[2];

gives you back an HTML element.

and you can call "click()" on it, because click is a method it provides.

click does return undefined (as expected)

Zo72
  • 14,593
  • 17
  • 71
  • 103