Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I simulate user clicking a link in JQuery?

Now there's quite a lot of similar-looking questions here, but I was wondering, what should I do, if I want not to just change the location of the window, but trigger whatever functions may be bound to the click event, and only change the href if they are ok with it, or just redirect, if there are no listeners.

For example:

var a = $('.edithost');
a.click(function() {return false;});

Should I click the link with mouse, it never takes me to the href, so just redirecting user to attr('href') would change the intended behavior of the page. Besides, clicking applies not only to links, but to, say, buttons too, in which case I would have to submit the form, etc.

So I was wondering, whether it is possible to emulate clicking an element, so that all the behavior of the browser is exactly the same, as if clicked with mouse?

There can be no listeners bound to a link.

Example:

var a = $('<a href="google.com">google</a>');
a.click();
a.trigger('click');

This won't take you to Google, I want to make it do.

Update: .click() won't work. trigger('click') too. preventDefault has nothing to do with this

like image 848
Fluffy Avatar asked Nov 28 '11 10:11

Fluffy


People also ask

How do you simulate a user click?

Method 1: Using the click() method: The click() method is used to simulate a mouse click on an element. It fires the click event of the element on which it is called. The event bubbles up to elements higher in the document tree and fires their click events also.

Can I click a button using jQuery?

The onclick function in jQuery can be used in HTML elements such as div, paragraphs, hyperlinks, and so on to accomplish the desired objective. A mouse event occurs when the button on the mouse is depressed.

Can we use Onclick in anchor tag?

Using onclick Event: The onclick event attribute works when the user click on the button. When mouse clicked on the button then the button acts like a link and redirect page into the given location. Using button tag inside <a> tag: This method create a button inside anchor tag.

How do you trigger a button on another page after clicking a link?

To create a button link to another page in HTML,just add <a> tag and wrap it around the simple Html button. Inside a <a> tag simply use href=“” attribute to give the path of the desired page. The output shows that, after clicking the “Click” button, you will be navigated to “Google” instantly.


2 Answers

In IE, you can call .click() on the element, but for standard browsers you need to simulate the click event by creating a native mouse event and then use dispatchEvent to trigger it.

I summed it all up in this jQuery 'plugin':

$.fn.simulateClick = function() {
    return this.each(function() {
        if('createEvent' in document) {
            var doc = this.ownerDocument,
                evt = doc.createEvent('MouseEvents');
            evt.initMouseEvent('click', true, true, doc.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
            this.dispatchEvent(evt);
        } else {
            this.click(); // IE
        }
    });
}

Now, just call:

$('.edithost').simulateClick();
like image 95
David Hellsing Avatar answered Nov 14 '22 23:11

David Hellsing


Try a[0].click();

This will execute the click method of the DOM element instead of triggering the event.

like image 21
ThiefMaster Avatar answered Nov 14 '22 21:11

ThiefMaster