Ruikp
Ruikp

Reputation: 57

How to click a button without name or ID?

How do I click on this button, without ID or Name:

<input type="submit" class="btn float_right btn" style="margin-top: 10px; margin-bottom: 10px;" value="Calcular" />

I've tryed:

document.getElementsByClassName("btn float_right btn").click()

and

document.getElementsByClassName("btn float_right btn")[0].click()

without sucess.

The console gives the error:

ERROR: Execution of script 'PP$1s' failed! document.getElementsByClassName(...).click is not a function

Upvotes: 0

Views: 1375

Answers (4)

Adam Poh
Adam Poh

Reputation: 1

You can check by using this for get correct DOM.

console.log(document.getElementsByClassName("btn float_right")[0]);

if there are other DOMs that have "classname" attribute name like it, you can not get coreect DOM.

Upvotes: 0

ghybs
ghybs

Reputation: 53280

Try to have the most specific selector, in order to avoid "clicking" on something else…

document.querySelector('input[value="Calcular"]').click();

Demo: https://jsfiddle.net/3v7hd2vx/420/

Upvotes: 1

user2575725
user2575725

Reputation:

document.getElementsByClassName("btn float_right")[0].click();
<input onclick='alert("You clicked!")' type="submit" class="btn float_right" style="margin-top: 10px; margin-bottom: 10px;" value="Calcular" />

Its's not an issue but class btn was repeated

Upvotes: 0

Nenad Vracar
Nenad Vracar

Reputation: 122077

You can use querySelector() and target one specific element by classes. If you want more specific selector you can use something like this input[type="submit"].btn.float_right

document
  .querySelector('.btn.float_right')
  .addEventListener('click', function() {
    console.log(this.value)
  })
<input type="submit" class="btn float_right btn" style="margin-top: 10px; margin-bottom: 10px;" value="Calcular" />

Upvotes: 0

Related Questions