0

I have this html code for displaying geolocation :

<span id="currentLat"></span>, <span id="currentLon"></span>

combined with javascript code it works perfectly. The problem is i want to display above value in a textfield.

I did try :

<input id="" value="<span id="currentLat"></span>, <span id="currentLon"></span>" size="30" />

but it give me error. How i add the value into the textfield value?

NB:

I add the javascript code used to know exactly what's wrong with the code, but maybe the code cannot be working in the text input field.

<script>
      window.onload = function() {
        var startPos;

        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            startPos = position;
            document.getElementById("startLat").innerHTML = startPos.coords.latitude;
            document.getElementById("startLon").innerHTML = startPos.coords.longitude;
          }, function(error) {
            alert("Error occurred. Error code: " + error.code);
            // error.code can be:
            //   0: unknown error
            //   1: permission denied
            //   2: position unavailable (error response from locaton provider)
            //   3: timed out
          });

          navigator.geolocation.watchPosition(function(position) {
            document.getElementById("currentLat").innerHTML = position.coords.latitude;
            document.getElementById("currentLon").innerHTML = position.coords.longitude;
            document.getElementById("distance").innerHTML =
              calculateDistance(startPos.coords.latitude, startPos.coords.longitude,
                                position.coords.latitude, position.coords.longitude);
          });
        }
      };

      // Reused code - copyright Moveable Type Scripts - retrieved May 4, 2010.
      // http://www.movable-type.co.uk/scripts/latlong.html
      // Under Creative Commons License http://creativecommons.org/licenses/by/3.0/
      function calculateDistance(lat1, lon1, lat2, lon2) {
        var R = 6371; // km
        var dLat = (lat2-lat1).toRad();
        var dLon = (lon2-lon1).toRad();
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
                Math.sin(dLon/2) * Math.sin(dLon/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d = R * c;
        return d;
      }
      Number.prototype.toRad = function() {
        return this * Math.PI / 180;
      }
    </script>
5
  • cannot add an element into an input Commented Jun 1, 2015 at 6:06
  • try single quotes? value="<span id='currentLat'></span>, <span id='currentLon'></span>" Commented Jun 1, 2015 at 6:07
  • @roullie. why don't you try first? Commented Jun 1, 2015 at 6:08
  • You id attribute is missing the double quotes. Change to this: <input id="" value="<span id='currentLat'></span>, <span id='currentLon'></span>" size="30" /> Commented Jun 1, 2015 at 6:17
  • sorry forgot to add thhe double quotes, just edit the code. Your code indeed adding the code to input field but the javascript function not working Commented Jun 2, 2015 at 15:16

3 Answers 3

1

Updated see the running code

<input value='<span id=\"currentLat\"></span>, <span id=\"currentLon\"></span>' size="30" />

 <input value="<span id='currentLat'></span>, <span id='currentLon'></span>" size="30" />

And if you want to set width and height then it should be

  style="height:100px;width:200px"
Sign up to request clarification or add additional context in comments.

2 Comments

like the comment above by Danish Bhayani. please test your code first before commenting
The code added into the text field but the javascript function not working, no latitude and longitude value appear.
0

You have there typos (unclosed quotes at ID attribute). Than you need to set just span's content, not whole elements.

<input id="result" value="" size="30" />

<script>

var res = document.getElementById('result');
var lat = document.getElementById('currentLat');
var lon = document.getElementById('currentLon'); 

res.value = lat.innerText + ', ' + lon.innerText;

</script>

https://jsfiddle.net/zc7pLcoq/

1 Comment

i try the snipet above but it only generate the comma value ( , )
0

You need to escape them properly -

<input value='<span id="currentLat"></span>, <span id="currentLon"></span>'>

Fiddle

1 Comment

same, the code added into the field but the javascript function not working

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.