Shirotzu San
Shirotzu San

Reputation: 27

How do click to a href with javascript?

I Want do a click to an existing <a href="">

Code :

var els = document.getElementsByTagName("a");
for (var i = 0, l = els.length; i < l; i++) {
    var el = els[i];
    if (el.href === 'https://www.messenger.com/t/someperson') {
        el.click;
    }
}

Upvotes: 1

Views: 962

Answers (3)

Maria
Maria

Reputation: 323

You can try window.location

var els = document.getElementsByTagName("a");
for (var i = 0, l = els.length; i < l; i++) {
    var el = els[i];
    if (el.href === 'https://www.messenger.com/t/someperson') {
        window.location = el.href;
    }
}

Upvotes: 1

Anish Silwal
Anish Silwal

Reputation: 980

Try this:

    var els = document.getElementsByTagName("a");
    for (var i = 0, l = els.length; i < l; i++) {
        var el = els[i];
        if (el.href === 'https://www.messenger.com/t/someperson') {

           el.click();
        }
    }

Upvotes: 1

Joe Attardi
Joe Attardi

Reputation: 4521

el.click is a function, you have to call it:

el.click();

Upvotes: 2

Related Questions