Skip to content

Instantly share code, notes, and snippets.

@germanny
Created November 4, 2019 20:54
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 germanny/ef6d04978318b0566369bf947b2721fb to your computer and use it in GitHub Desktop.
Save germanny/ef6d04978318b0566369bf947b2721fb to your computer and use it in GitHub Desktop.
JavaScript - Redirect to <option> value on form submission
{
  document.addEventListener("submit", (ev) => {
    const form = ev.target;
    const value = form.subject.value;

    // Return if doesn't contain class
    if (!form.classList.contains("my-class-name")) {
      return;
    }

    // Prevent Default
    ev.preventDefault();

    // Return if no value
    if (!value || value === "0" || value === "") {
      return;
    }

    // Redirect to search page
    window.location = value;
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment