Home > Blockchain >  I need to insert a value in an input field of an html form through javascript
I need to insert a value in an input field of an html form through javascript

Time:03-07

HTML code

<div id="formulario">
    <h1>Posicionamento de CEO</h1>
    <form method="POST" style="width: 500px;">   

<div >
   <label  for="id_identification">Identificação</label>
        
<div >enter code here
   <input type="text" name="identification" maxlength="256"  required="" id="id_identification">
</div>
</div>
<div >
   <label  for="id_geolocation">Geolocalização</label>
       <div >
            <input type="text" name="geolocation" maxlength="100"  required="" id="id_geolocation">
</div
        <button type="button" onclick="start()" >Clique aqui para carregar 
         mapa</button>
    </form>
</div>

Javascript code that opens the map and captures the coordinate when I click on the map.

async function sendCoordsToMyApi(evt) {
    const coords = {
      lat: evt.latLng.lat(),
      lng: evt.latLng.lng()
    };

    console.log("vou enviar as coordenadas para api:", coords);
    alert(JSON.stringify(coords));

I need to enter the coordinate of the constant coords in the form in the input field id="id_geolocation" can you help me?

CodePudding user response:

You can use document.getElementById to get the element you are trying to add text to

coords = ...;
document.getElementById("id_geolocation").value = JSON.stringify(coords);
  • Related