javascript - jQuery value not getting assigned from ipinfo when expected -
i'm using:
//function obtain current location, used display relevant contact information $.get("http://ipinfo.io", function(response){ $("#address").html(response.region);//http://jsfiddle.net/zk5fn/2/ $("input[id=address]").val(response.region); }, "jsonp");
to current location contact page. aim can use if address== display various contact numbers etc.
this function updates value of hidden field. i've checked debugger , it's working fine.
i'd value can play it's blank or empty.
i have
$(document).ready(function(){ var testloc = $('#address').val(); if(testloc == ""){ alert("noes!!!"); } });
which i've placed in same script block within head see if results different, again, it's blank , returns noes alert.
i though waiting document load i'd values assigned/set.
can suggest solution please? i'm sure there's simple fix , it's killing me
edit: html
<input type="hidden" name="address" id="address"/>
edit solution:
<script> $(function() { $.ajax({url:"http://ipinfo.io",datatype:'jsonp'}) .success(function(response){ $("#address").val(response.region); $("input[id=address]").val(response.region); var testloc = $('#address').val(); if(testloc == ""){ alert("noes!!!"); }else{ alert("jquery can suck my...") } }); }); </script>
i'd recommend putting $.get
request inside document.ready function.
note ready function called dom ready, if ajax returns before then, element you're filling value may not ready.
$(function() { // shorthand $(document).ready( $.get({url"http://ipinfo.io",datatype:'jsonp'}) succsss(function(response){ // response }); });
fiddle: http://jsfiddle.net/v6dbb/
Comments
Post a Comment