I want to run the following html5 geolocation javascript before submitting the form. Basically I'm after this process: the geolocation popup appears when submitting the form, the user clicks allow and then the form will be processed.
function getGeoLocation() {
navigator.geolocation.getCurrentPosition(setGeoCookie);
}
function setGeoCookie(position) {
var cookie_val = position.coords.latitude + "|" + position.coords.longitude;
document.cookie = "lat_lng=" + escape(cookie_val);
}
And I'm trying to accomplish this via an onclick event from the form:
<%= form_tag search_path, :method => "get" do %>
<%= text_field_tag :search, params[:search], autofocus: true,
placeholder: "Enter keyword to search" %>
<%= submit_tag "Search", name: nil, onclick: "getGeoLocation();", :style => "display: none;" %>
<%end%>
But unfortunately nothing is happening, I'm receiving no errors under browser inspector as well! Any ideas on how to implement this?
Update 1
I'm also using autocomplete to submit the form:
$(document).ready(function() {
$("#search").autocomplete({
source: "/search_suggestions",
autoFocus: true,
select: function(event, ui) {
$(event.target).val(ui.item.value);
$('#search').closest("form").submit();
return false;
}
});
});
If I use @Alex Kojin it redirects me to another form a have inside another tab.