Eddier10
Eddier10

Reputation: 11

Javascript code to press a particular button on a webpage

I have an HTML form to upload two pictures separately (So I have two submit buttons) after selection automatically without the user having to press a submit button. From my research I'm supposed to use

this.form.submit();

The pictures will be saved in separate locations too. I want to know how to target a particular button with either its name or id.

<input type = "file" name = "headshot1">
<input type = "submit" name = "submit1">

<input type = "file" name = "headshot2">
<input type = "submit" name = "submit2">

Then I have a php code to store the two pictures in two different places

Upvotes: 1

Views: 60

Answers (2)

Ohgodwhy
Ohgodwhy

Reputation: 50787

One way is by utilizing the querySelector() to get your object, and then chaining addEventListener off of it.

document.querySelector('button[name="myButton"]').addEventListener('click', function(){
    //stuff
});

Upvotes: 1

Dennis Beier
Dennis Beier

Reputation: 373

You can directly submit the form:

 document.getElementById("myFormId").submit();

If you still want to click the button go with:

 document.getElementById("buttonid").click();

Upvotes: 0

Related Questions