1

HTML

<form>
  <select name="select">
     <option value="option1">Option 1</option>
     <option value="option2">Option 2</option>
  </select>
  <input type="submit" onclick="processForm(this.form)">
</form>

JS

function processForm(form) {
  console.log(form)
}

I can successfully get the form as HTMLElement but cannot seem to find easy way to get selected value

Tim Blount
  • 35
  • 5
  • What do you want to do with the form? Do you only want the values or you want to send it to a server as a POST method?? – inoabrian Nov 12 '14 at 19:30
  • there is a selectIndex attribute. – GeekByDesign Nov 12 '14 at 19:31
  • Possible duplicate of [Get selected value in dropdown list using JavaScript?](http://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) – totymedli Jan 11 '17 at 22:44

1 Answers1

3

Since you're sending the form object itself on submit, getting data is easy in vanilla JS. Modify your function to:

function processForm(form) {
  var selected = form.select
  console.log(selected.value)
}

Note that select is not some special property of the form, it's just because that's what you gave the name attribute for the select element.

elzi
  • 5,442
  • 7
  • 37
  • 61
  • I'd change your HTML to give the ` – elzi Nov 12 '14 at 19:34