-1

I'm working on a form that checks which option in <select> element is selected and then redirect to a page after button is clicked.

I've tried from jQuery get value of select onChange, but it only helps when option is selected immediately.

function getval() {
  if (sel.value == "2") {
    window.location.href = "payment1.html";
  } else if (sel.value == "3") {
    window.location.href = "payment2.html";
  }
}
<select id="payment-select">
  <option value="1">Select payment option</option>
  <option value="2">Payment1</option>
  <option value="3">Payment2</option>
</select>
<br>
<input type="button" onclick="getval()" class="button" value="Continue">

How would I connect this script to my <input type="button" onclick="getval()" class="button" value="Continue">?

Quazey
  • 81
  • 9
  • `function getval() { let loc = ["","","payment1.html","payment2.html"][document.getElementById("payment-select").value]; if (loc) location.href=loc; }` - sel is undefined – mplungjan Mar 03 '20 at 14:49
  • Voting to close since the issue is _caused by a typo or problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers._ – mplungjan Mar 03 '20 at 14:50

1 Answers1

1

sel variable is undefined. You need to store the value of the selected option.
You can select it like this: document.getElementById('payment-select').

function getval() {
  var sel = document.getElementById('payment-select');

  if (sel.value == "2") {
    window.location.href = "payment1.html";
  } else if (sel.value == "3") {
    window.location.href = "payment2.html";
  }
}
<select id="payment-select">
  <option value="1">Select payment option</option>
  <option value="2">Payment1</option>
  <option value="3">Payment2</option>
</select>
<br>
<input type="button" onclick="getval()" class="button" value="Continue">
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Azametzin
  • 5,223
  • 12
  • 28
  • 46