1

so I've tried this very hard but can't figure it out,

I have a button like this

<button onclick="signUp(document.getElementById('username').value,document.getElementById('email').value)">Sign up</button>

and I want to pass the values it's requesting from some inputs into a variable or variable array so I can handle them like this (example)

var signUp = function(e){
    alert(e);
}

(This is just simplified but it proves my point!) Does anyone know how to do this though, I've been having a hard time figuring it out!

Thanks in advance, Magn0053

9
  • Just add an another argument var signUp = function(e, f) {... Commented Jun 24, 2015 at 14:21
  • Yes but I would like to use them all in the "e", like some sort of array Commented Jun 24, 2015 at 14:22
  • Then you have to pass just an array signUp([val1, val2]) Commented Jun 24, 2015 at 14:23
  • Or access arguments, but you're really complicating it, just use two arguments instead. Commented Jun 24, 2015 at 14:24
  • Rather than passing the values to the function, you can also get it in your function itself - I do not understand the reason of passing value of some elements in function? Commented Jun 24, 2015 at 14:24

4 Answers 4

2

You can surround by { } to pass them as an object, or [ ] to pass them as an array. However, all of your arguments are document values, your function could just reference them itself.

Sign up to request clarification or add additional context in comments.

Comments

1

You may take @adeneo's idea or, just send'em in an array like this

signUp([document.getElementById('username').value, 
    document.getElementById('email').value])

2 Comments

This is really bad practice. Say you want to use the signup on another page - now you have to include all this html again rather than just include the same js file. What's more, in an array you have no idea what each element is.
what html are you talking about?
0

Basic Solution

The most basic modification is to use [] around the values you are passing to signUp.

var signUp = function(e) {
  alert ("username:" + e[0]);
  alert ("email:" + e[1]);
}
<input type="text" id="username">
<input type="text" id="email">
<button onclick="signUp([document.getElementById('username').value,document.getElementById('email').value])" id="submit">Sign up</button>

Best Practice

You don't really want to be using javascript in your DOM so first modify your signUp function:

signUp = function() {
    var username = document.getElementById('username').value;
    var email = document.getElementById('email').value;
}

Finally, remove the onclick from your HTML entirely and replace it with this javascript:

document.getElementById('submit').onclick=signUp;

var signUp = function() {
  var username = document.getElementById('username').value;
  var email = document.getElementById('email').value;
  alert("username:" + username);
  alert("email:" + email);
}

document.getElementById('submit').onclick = signUp;
console.log("fred");
<input type="text" id="username">
<input type="text" id="email">
<button id="submit">Sign up</button>

Comments

0

You can achieve this as such:

<button onclick="signUp({username:document.getElementById('username').value,email:document.getElementById('email').value})">Sign up</button>

In js, you can access them like:

var signUp = function(e){
    console.log(e.username, e.email);
}

1 Comment

Why would you send an array of objects? Just send an object: {username: document.blahblah, email: document.blahblah}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.