Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:09:03+00:00 2026-05-23T07:09:03+00:00

2 Questions, I have a form that lists 3 locations. Upon submitting the form

  • 0

2 Questions, I have a form that lists 3 locations. Upon submitting the form I would like the variable “event_location” to equal whichever location was chosen in the drop down menu. The only thing I don’t understand is, what if the user doesn’t select a location at all, i.e. the user just uses the default location(the first one that shows up). I don’t think my javascript code accounts for this. Below I have included both the html and javascript.

<select name="location" id="event_location" onchange='getSelectedValue();'>
<option value="sandiego">San Diego</option>
<option value="losangeles">Los Angeles</option>
<option value="sanfrancisco">San Francisco</option>
</select>

function getSelectedValue() {

var event_location = document.getElementById('event_location').value;


}

The function getSelectedValue() only happens when the ‘onchange’ event is triggered. So if they are satisfied with San Diego as their first option, they won’t trigger the onchange event. Should I just declare event_location = ‘sandiego’ outside of the function?

The other tricky part is based on which event_location they pick(san diego, los angeles, or san francisco), my javascript changes which URL they are directed to. How can I change the URL to account for which event_location they picked. For example if they picked san diego, once they hit the search button it will take them to: http://myurl.com/sandiego. Here is the html and javascript I have so far for my second function:

<input type='button' value='Search' onclick='eventViewer();' />

function eventViewer() {
   function getSelectedValue() {
       var event_location = document.getElementById('event_location').value;
       }

   if (event_location = 'losangeles') {
      window.open( 'http://...' )
      }
   else if (event_location = 'sanfrancisco') {
      window.open( 'http://...' )
      }
   else {
      window.open( 'http://...' ) //link to san diego
      }
   }

Rather than creating 2 seperate functions you can just grab the values from a dropdown box using the standard

var event_location = document.getElementById('event_location').value;

This will grab whatever value is in the drop down menu. So you don’t have to worry about variable scope with 2 different functions, you can just declare all of your variables upon 1 ‘OnClick’ event.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:09:03+00:00Added an answer on May 23, 2026 at 7:09 am

    In answer to the first part of your question, instead of using onchange attached to the select element, you can change to using onsubmit attached to the form element, that way, the variable will be populated regardless of whether or not it has been changed:

    <form action='/process_form' onsubmit="getSelectedValue();">
    ...
    </form>
    

    Could you clarify the second part of your question into an actual question – I’m not sure what you’re asking at the moment.

    EDIT:

    Two things which might make the second part of your question easier:

    1. If there are no other dependencies then make the ‘value’ of each select item in the dropdown the URL to which you want to send the person. That way you can simply read the URL from the select value and send them there directly without any need for translation using a case statement.

    2. Try using a switch statement instead of a cascading if/else, it’s easier to read and to debug. You can also reduce the amount of code and improve its clarity by moving the action (i.e. window.open) to the bottom of the switch statement and using each switch only to set the variable:

    e.g.

    switch(event_location)
    {
      case "san_francisco":
         var url = 'http://sanfrancisco.com'
         break;
      case "san_diego":
         url = 'http://sandiego.com'
         break;
      default:
         alert('please choose a city')
    }
    url ? window.open(url) : null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a special sub-node that could be attached to other
I have a form which for some questions would ask if something was included
I have a WinForms data entry form that will have upwards of 1500 questions.
I have a ajax form that populates select lists with values based on the
this is a simple question. I have a form that is being validated using
This is probably a basic html/css question... I have a simple one-button form that
If I have a form of about 40 questions, how do I apply the
I have a relatively simple form which asks a variety of questions. One of
I have a design questions about this scenario: Suppose you have a form with
I have this form with a JS function to verify that there are no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.