0

The reason i need to execute the event behind a a element is because i'm working on some kind of autologin, however one of the sites it has to work with is https://create.kahoot.it/#login?a=1&next= the problem is, their login uses an a element for the sign in button, however when i select that element and execute a .click on it it simply doesn't do the same thing as to what happens when a user clicks on it. I hope someone could answer this question since i couldn't find anything close to this issue anywhere.

Also for the convenience of whoever helps, to select the element from the console you could use: document.getElementById('sign-in').getElementsByTagName('a')[1];

The way to solve this issue has to be either javascript or JQuery, preferably just plain javascript.

knowndead
  • 80
  • 8
  • Show us a minimal working example of code. – Scott Marcus Jan 04 '17 at 01:14
  • I just answered today the exact same question here: http://stackoverflow.com/questions/41447327/triggering-click-on-a-link-doesnt-change-location-hash/41447448#41447448 – Dekel Jan 04 '17 at 01:16
  • If there was i wouldn't be asking, the working part would be manually pressing sign in on kahoot. – knowndead Jan 04 '17 at 01:17
  • Call the method for that event, for example `element.click()` will trigger the click event for `element`. – Spencer Wieczorek Jan 04 '17 at 01:17
  • @ Spencer Wieczorek so how about an a element? which in my case doesn't even have an url – knowndead Jan 04 '17 at 01:19
  • @knowndead It doesn't matter if it's an `a` element. – Spencer Wieczorek Jan 04 '17 at 01:21
  • it doesn't do anything @Spencer Wieczorek e = document.createEvent('MouseEvents'); e.initEvent("click", true, true); $('a[class="btn register"]').get(1).dispatchEvent(e) also doesn't work which was Dekel his idea(refering to his linked post) It does work on the register link, just not on the sign in link – knowndead Jan 04 '17 at 01:26

1 Answers1

0

Try this:

document.getElementsByClassName('btn register')[0].click();

This basically is selecting the anchor by its class name and fire click event manually.

UPDATE:

Alright I did some more research and it seems there is another way of triggering that click handler and it is to set href on the window:

var a = document.getElementsByClassName('btn register')[1];

window.location.href = a.href;

I've tried it and seems it is doing the job.

Yaser
  • 5,609
  • 1
  • 15
  • 27
  • I see i made a mistake in my original post it is element 1 so that would be document.getElementsByClassName('btn register')[1].click(); which doesnt work(it is the sign in button) – knowndead Jan 04 '17 at 01:34
  • I've tested my code and it works, what do you mean by [1]? @knowndead – Yaser Jan 04 '17 at 01:35
  • Well it is true that document.getElementsByClassName('btn register')[0].click(); works, but that is actually the register buttton on the top right, i need it to work for the sign in button which is a slightly different scenario where your code won't work. Also with element 1 i ment the button with the text sign in – knowndead Jan 04 '17 at 01:39