views:

457

answers:

1

I have a form with a hidden input that saves user's current zoom level

<input type="hidden" name="zoom" value="15" id="zoom" /> <!-- default value="" -->

However, when I try to use the zoom value from the input after I submit my form it doesn't use the input's value

var zoom = $("#zoom").val();
var point;
var gmap = new GMap2(document.getElementById("gmap"));

if (zoom == "") {
    point = new google.maps.LatLng(latitude,longitude);
    gmap.setCenter(point, 13);
} else {
    console.log(zoom); //shows 15
    point = new google.maps.LatLng(latitude,longitude);
    gmap.setCenter(point, zoom); //here the zoom is undefined
}
+4  A: 

The value of 'zoom' from the input field is a string, and the function requires a number. Use parseInt() to cast the field value as a numeric value.

var zoom = parseInt($("#zoom").val());
var point;
var gmap = new GMap2(document.getElementById("gmap"));
// point assignment is abstracted from your zoom level logic
point = new google.maps.LatLng(latitude,longitude);

gmap.setCenter(point, (typeof zoom == 'number') ? zoom : 13);
bdl