Skip to content

Instantly share code, notes, and snippets.

@MattMcAdams
Created January 28, 2021 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattMcAdams/cd1574a51281e37897e3789c617be3ec to your computer and use it in GitHub Desktop.
Save MattMcAdams/cd1574a51281e37897e3789c617be3ec to your computer and use it in GitHub Desktop.
Redirect based on form selection
<form id="myForm">
<select id="select">
<option value="pageSlug1">Page Title 1</option>
<option value="pageSlug2">Page Title 2</option>
<option value="pageSlug3">Page Title 3</option>
</select>
<button onclick="selectRedirect()">Submit</button>
</form>
// Prevent submit button from refreshing page
document.getElementById("myForm").addEventListener('submit', handleForm);
function handleForm(event) { event.preventDefault(); }
function countyRedirect() {
let field = document.getElementById("select");
let value = field.value;
console.log(field, value);
window.location.href = "https://example.com/" + value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment