2

Im trying to assign a value for my ion-input element via JS. here is my code:

HTML:

<ion-input type="text" placeholder=" Enter a location" style="padding-left: 10px;" id="geolocation"></ion-input>

JAVASCRIPT:

function onSuccess(position) {
        var element = document.getElementById('geolocation').value = position.coords.latitude  + ',' + position.coords.longitude;
        //element.value =  position.coords.latitude  + ',' + position.coords.longitude + element.value;

    }

I cannot change the value of my input element, I want to put the position.coords.latitude + ',' + position.coords.longitude as the value of my input element.

Hoping for a quick answer. Thanks!

1
  • 1
    why not use [(ngModel)] and bind the object directly to the input ? without touching the DOM directly like you are doing ? Commented Apr 22, 2016 at 23:22

3 Answers 3

2

Assuming that you're receiving the latitude and longitude correctly in array form, the code should look like this:

function onSuccess(position) {
        document.getElementById('geolocation').setAttribute("value", latitude + ', ' + longitude);
}

Test here: https://jsfiddle.net/nabtron/qy96n44k/

Explanation:

Here we simply called the element by using its Id and set its attribute value as explained here: https://nabtron.com/javascript-tips/

Sign up to request clarification or add additional context in comments.

2 Comments

I found a solution I did var element = document.getElementById('geolocation'); element.value = position.coords.latitude + ',' + position.coords.longitude it worked :D
I'm glad that you fixed it! but it's the same as this answer plus what you were trying earlier, you might have changed other things in code too that might be having issue earlier.
1

try something like:

[(ngModel)]="position.coords.latitude  + ';' + position.coords.longitude"

Dom manipulation will work, but it's not the right way, see how use the solution below:

HTML

<ion-input type="text" placeholder=" Enter a location" style="padding-left: 10px;" id="geolocation" [(ngModel)]="position.coords.latitude  + ';' + position.coords.longitude"></ion-input>

Javascript:

...

export class Geolocation {

   //{ position : { coords : { latitude : ... , longitude : ... } } }
   position : any;

   ...
}

Comments

0

Try this one.

document.getElementById("geolocation").value=
     (position.coords.latitude  + ',' + position.coords.longitude);

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.