I have 3 inputs, and I want to put their values into the form onsubmit attribute function. Here's my current code. I would like a way to do this that won't require me to create 3 variables using getElementById()
or querySelector()
, preferably in the HTML file, but if it's impossible, I'll accept defeat.
<form id="form" onsubmit="addUserCredentials(userID, username, email)">
<input type="text" id="userID" name="userID"></input>
<label for="userID">User ID</label>
<input type="text" id="username" name="username"></input>
<label for="username">Username</label>
<input type="text" id="email" name="email"></input>
<label for="email">Email Address</label>
<button>Submit</button>
</form>
p.s. here's script.js if you want to look (and yes, i'm using firebase)
function addUserCredentials(userId, username, email) {
db.ref('users/' userId).set({
username: username,
email: email
});
}
CodePudding user response:
The inputs will always be available in the form object with its name as the key and does not require to be passed as parameters so,
<form id="form" onsubmit="addUserCredentials()">
function addUserCredentials() {
const userId = this.userID.value
const username = this.username.value
const email = this.email.value
}
this
as the form element.