user2582318
user2582318

Reputation: 1647

press a button without id or name with javascript

i need to press a button in one page, but the button dont have id, name or anything else

only this is the code of the button:

<input type="submit" value="continue">

there is a way to press it?

i tried this code from here:

 function clickButton(val)
{
var buttons = document.getElementsByTagName('input');
  for(var i = 0; i < buttons.length; i++) 
  {
     if(buttons[i].type == 'button' && buttons[i].value == val) 
     {
          buttons[i].click();
          break; //this will exit for loop, but if you want to click every button with the value button then comment this line
     }
  }

}

but dont worked too, nothing happen, the page refresh but still in the same place

Upvotes: 0

Views: 8502

Answers (2)

Oliver
Oliver

Reputation: 9002

Your input is type "submit", however your javascript is looking for type "button". Try changing to this:

function clickButton(val)
{
    var buttons = document.getElementsByTagName('input');
    for(var i = 0; i < buttons.length; i++) 
    {
       if(buttons[i].type == 'submit' && buttons[i].value == val) 
       {
           buttons[i].click();
           break; //this will exit for loop, but if you want to click every button with the value button then comment this line
       }
    }
}

And ensure that you pass the correct value when calling this function:

clickButton('continue');

Upvotes: 1

karaxuna
karaxuna

Reputation: 26940

Type of your button is "submit" not "button":

<input type="submit" value="continue">

Change this:

buttons[i].type == 'submit'

Upvotes: 0

Related Questions