0

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>
0

1 Answer 1

5

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){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.