Jackson
Jackson

Reputation: 820

jQuery: autoclick link?

I know this question has been asked many times before and there are countless answers for it but the issue that I have is that I have no idea why I cannot autoclick a link on page load!

I have used the same process over 20 times before without any issue but now it doesn't work...

Here is a fiddle with the issue: https://jsfiddle.net/1xy0f291/

and this is my code:

$(document).ready(function(){
alert('hello');
  $('#pop').trigger('click');
   $('#pop').click();
});

anyone has any idea why this is happening?

Upvotes: 3

Views: 117

Answers (4)

Wasif Shahjahan
Wasif Shahjahan

Reputation: 376

try this :

document.getElementById('pop').click()

updated fiddle : Fiddle

Upvotes: 1

Karthikeyan
Karthikeyan

Reputation: 347

try this document.getElementById('pop').click()

Upvotes: 0

Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22031

Add some small code fix, use eq():

 $('#pop').click(); -> $('#pop').eq(0).click();
 $('#pop').trigger('click'); -> $('#pop').eq(0).trigger('click');

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Basically $().click() will invoke the click event handler bound with it. Not the natural click.

For invoking the natural click you have to access the plain node object and call its click() function,

 $('#pop')[0].click();

DEMO

Upvotes: 9

Related Questions