Click all buttons on page with same class Javascript

11,033

You are using getElementsByName instead of getElementsByClassName

 var buttons = document.getElementsByClassName('UnFollowUser');

 for(var i = 0; i < buttons.length; i++)  
     buttons[i].click();
Share:
11,033
Savant
Author by

Savant

Updated on June 28, 2022

Comments

  • Savant
    Savant almost 2 years

    I'm trying to click all buttons on a page with the class "btn btn-primary UnFollowUser".

    Here is the script I have tried using

    var buttons = document.getElementsByName('UnFollowUser');
    
    for(var i = 0; i <= buttons.length; i++)  
       buttons[i].click();
    

    But that throws the error:

    VM336:5 Uncaught TypeError: Cannot read property 'click' of undefined(…)(anonymous function) @ VM336:5InjectedScript._evaluateOn @ VM158:878InjectedScript._evaluateAndWrap @ VM158:811InjectedScript.evaluate @ VM158:667

    Any ideas?