2

I would like to fill input field with javascript ipinfo API response. I am ready to fill a PRE but not an INPUT. I need to send it by a form in order to save this information.

Please see it here http://jsfiddle.net/ak2txped/2/

$(window).load(function(){
  $.get("http://ipinfo.io", function(response) {
    $("#ip").html(response.ip);
    $("#code").html(response.country);
    $("#address").html("Location: " + response.city + ", " + 
    response.region);
    $("#details").html(JSON.stringify(response, null, 4));
  }, "jsonp");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="details"></pre>
<form id="details" action="/action_page.php" method="post">
  Details: <input type="text" name="fname" value="pre"><br>
  <input type="submit" value="Submit">
</form>

Thanks in advance

Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33
  • You cannot repeat ids. You've repeated the `details` id. https://stackoverflow.com/questions/9454645/does-id-have-to-be-unique-in-the-whole-page – Taplar Jun 04 '18 at 14:58
  • you are right, I have solved, but I can't fill this API response into input form... – Raul Taboraz Jun 04 '18 at 15:02

1 Answers1

0

You need to give input id

<input type="details" name="details" value="pre" id="fname">

then set the value using JQuery from the response.

$("#fname").val(response.org);

working link

http://jsfiddle.net/ak2txped/5/

Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33