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.