Rune Stake
Rune Stake

Reputation: 11

JQuery Issue Code, Trying To click a button programmatically but it isn't work - No id/class

Heres my current attempt:

<div class="links">
    <a href="/login">Login</a>
    <a href="/register">Register</a>
</div>

My JQuery For This Attempt Is:

setTimeout (
  function()
  {
    alert("Clicking Now");
    $('.links a:contains("Register")').trigger('click');
  }, 2000);

This doesn't seem to click the href link after 2 seconds... Any help please?

Upvotes: 0

Views: 26

Answers (1)

Bricky
Bricky

Reputation: 2745

jQuery has a built in function $(el).click() try using that first.

Certain browsers limit your ability to do actions that would typically have to be taken by the user (click, copy, paste), but you can still trigger/broadcast the events so that anything triggered by an eventListener can pick it up.

For this particular goal, I think it would be more appropriate to redirect the user to your login page, using something like window.location.href = '/login'

Upvotes: 1

Related Questions