0

I've got a basic javascript form creation script right here

var loginDiv; var loginForm; var formUserInput; var formUserPass; var formSubmit; loginDiv = document.createElement('div'); loginDiv.id = 'loginDiv'; loginForm = document.createElement("form"); loginForm.setAttribute('method', "post"); loginForm.setAttribute('action', 'JavaScript:doSomething()'); formUserInput = document.createElement("input"); formUserInput.setAttribute('type', "text"); formUserInput.setAttribute('name', "username"); formUserPass = document.createElement("input"); formUserPass.setAttribute('type', "text"); formUserPass.setAttribute('name', "password"); formSubmit = document.createElement("input"); formSubmit.setAttribute('type', "submit"); formSubmit.value = "Submit"; loginForm.appendChild(formUserInput); loginForm.appendChild(document.createElement("br")); loginForm.appendChild(formUserPass); loginForm.appendChild(document.createElement("br")); loginForm.appendChild(formSubmit); loginDiv.appendChild(loginForm); document.getElementById('game').appendChild(loginDiv); 

my question is; How do I pass the values of the text-fields into the doSomething() function?

2 Answers 2

2

You can get the value out of each of your dom nodes individually like so:

formUserInput.value 

Instead of using this:

loginForm.setAttribute('action', 'JavaScript:doSomething()'); 

You could bind an event handler like this:

loginForm.addEventListener("submit", function(){ doSomething(formUserInput.value, formUserPass.value) }) 
Sign up to request clarification or add additional context in comments.

Comments

1

You could pass it in as an object

Var formValues = { "username": document.getElementById(set an id).value, ... } 

Then pass the form values in as an object. You can access them as formValues.username etc.

Edit: Nick's suggestion to put a listener on submit is more conventional, I still suggest putting them in one nice tight object and pass it there.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.