Martel Benjamin
Martel Benjamin

Reputation: 150

Click on a button in the console doesn't work

I have a button to change page but when I want to click on it with the console it doesn't work. But with the mouse it works.

This is the plunker : http://plnkr.co/edit/shr2xhNHzcYwTBPJlisQ?p=preview

And I do this in the console : $('#language_us').click(); or $('#language_fr').click(); but it doesn't change page.

Have you an idea ? Thanks

Upvotes: 2

Views: 2123

Answers (3)

Shaunak D
Shaunak D

Reputation: 20626

You are using anchors <a> and not button <button>.

To fire click on <a> you need to use the DOM element, jQuery object $() does not work.

Use,

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

.trigger() or .click() triggers a click handler defined explicitly. You need the native click event which is the default functionality of an anchor. So you need to use HTMLElement Object.

If you would have wrote :

$('#language_fr').click(function(){
  alert('..')
})

The

$('#language_fr').click()

would have fired the alert().

Upvotes: 4

Romeo
Romeo

Reputation: 1185

Maybe try to change the tag from a to button or put a button tag inside the a tag and press that.

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Try this:

$('#language_us').trigger('click');
$('#language_fr').trigger('click');

Or may be try:

$('#language_us').get(0).click();
$('#language_fr').get(0).click();

Upvotes: 2

Related Questions