Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This question already has an answer here:

Let's say I have the following:

Select a Number:
<select name="Quantity" id="selection"> 
<option value="Value1">1</option> 
<option value="Value2">2</option> 
<option value="Value3">3</option> 
<option value="Value4">4</option> 
</select>

Enter Something:
<input type="string" name="Input1"/></input>

One is a dropdown menu, and one is a box you can type text in.

I want to take what the user selects/types in and assign it to a javascript variable. I then want to pass the variable to a javascript file later in the page.

I believe I can figure out the latter part, but how would I go about assigning the user selection/input to a variable?

Can I do something like var one = document.Quantity.Value; var two = document.Input1.Value; ?

share|improve this question

marked as duplicate by matiash, Robbert, Neil Lunn, David Pope, kapa javascript Jun 8 '14 at 12:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 2 down vote accepted

You can use querySelector() method :

var select_value = document.querySelector('#selection').value
var input_value = document.querySelector('input[name="Input1"]').value 

Note : querySelector('input[name="Input1"]') takes the first input with attribute name = "Input1",you can use querySelectorAll to catch them all and loop though the collection.

You can also do it in the other way :

var select_value = document.getElementById('selection').value
var input_value = document.getElemenstByName('Input1')[0].value 
share|improve this answer
1  
Thanks! I'll try it out and let you know if I have any problems. Appreciate the help and will accept your answer shortly :) – user2946613 Jun 6 '14 at 3:11
    
@user2946613 : Glad to help you – potashin Jun 6 '14 at 3:26
1  
Just wanted to say I tried it and it worked! THANK YOU SO MUCH :) – user2946613 Jun 6 '14 at 4:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.