0

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; ?

potashin
  • 44,205
  • 11
  • 83
  • 107
user2946613
  • 131
  • 2
  • 9

1 Answers1

2

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 
potashin
  • 44,205
  • 11
  • 83
  • 107