Reputation: 2522
What am I missing here? It seems simple but I can't figure out why the 2nd alert shows "undefined" for the var id
<input type='button' class='getLoc' id=‘s13' value=‘s13’>
<script>
$(function(){
$('.getLoc').click(function(){
var id = $(this).attr('id');
alert(id); //<-- shows s13
navigator.geolocation.getCurrentPosition(function(position,id){
var lat = position.coords.latitude;
var lon = position.coords.longitude;
alert(lat+","+lon+","+id); //<-- shows undefined for id
});
});
});
</script>
Upvotes: 0
Views: 34
Reputation: 414016
It's because you included id
as a parameter to the callback function. That's the id
that your alert()
will refer to, and since only one argument is passed to the callback the value is undefined
.
It should just be
navigator.geolocation.getCurrentPosition(function(position){
Upvotes: 5