Phil Cho
Phil Cho

Reputation: 121

How to pass inputs from one function to another using .apply method?

For my assignment, I have to read 3 inputs. Once I have the input, I have to pass them as arguments to the addNewPost function. I understand that I have to use the .apply function, however, I am confused on how to do this even after looking at various sources online.

function handleFormSubmit() {

    var username = document.getElementById("input-username").value,
        password = document.getElementById("input-password").value,
        picture = document.getElementById("input-picture").value;

    addNewPost.apply(this, arguments);
}

function addNewPost(username, img_src, caption) {

}

Am I correctly reading the inputs, and if I am, how do I pass them as arguments to addNewPost?

Upvotes: 0

Views: 113

Answers (2)

Kavindra
Kavindra

Reputation: 1707

If using apply is not a requirement, you can simply pass them into the function;

addNewPost(username, password, picture);

Upvotes: 1

Martin E
Martin E

Reputation: 219

You could fill an array with your form data (parameters for your function)...

var args = [
        document.getElementById("input-username").value,
        document.getElementById("input-picture").value,
        document.getElementById("input-password").value
    ];

    addNewPost.apply(this, args);

Upvotes: 0

Related Questions