1

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;
    }
}
1
  • 2
    Call click as a function: el.click(). MDN Commented Jan 18, 2016 at 16:56

3 Answers 3

2

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

el.click();

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much just what I wanted.
1

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();
        }
    }

2 Comments

Thanks very much appreciate your help
mention not. don't forget to mark it as answer and to vote it if it solved your problem.
1

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;
    }
}

4 Comments

well this would work untill you need to do some javascript when the link is clicked..
I wonder then why it does not work document.getElementsByTagName('a').click();
Thanks but this only makes me redirect to that url, what I wanted was to click so it was el.click ();
well document.getElementsByTagName('a') returns an array and an array does not have a methon click(). So this works document.getElementsByTagName('a')[0].click(); not that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.