how to get user location based on IP address using javascript ? how to display checkbox if user is coming from other country and hide the checkbox if the user is not coming from other country ?
Asked
Active
Viewed 1,141 times
-2
-
1https://stackoverflow.com/questions/3489460/how-to-get-visitors-location-i-e-country-using-geolocation – null Mar 27 '18 at 17:34
1 Answers
0
You can get the user's country name using ipapi API
Response
{
"ip": "103.209.196.6",
"city": "Dhaka",
"region": "Dhaka Division",
"region_code": "C",
"country": "BD",
"country_name": "Bangladesh",
"continent_code": "AS",
"postal": "1000",
"latitude": 23.7231,
"longitude": 90.4086,
"timezone": "Asia/Dhaka",
"utc_offset": "+0600",
"country_calling_code": "+880",
"currency": "BDT",
"languages": "bn-BD,en",
"asn": "AS134180",
"org": "Md. Shariful Islam T/A BRISK SYSTEMS"
}
so from the response, you got country_code and country_name you can use country_code/country_name.
Code
$.getJSON('https://ipapi.co/json/', function(result)
{
$("input[name='country']").each( function ()
{
if(result.country_name ==$(this).val())
{
$(this).parent().hide();
}
}
)});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div >
<label><input type="checkbox" name="country" value="United States"> United States</label>
<label><input type="checkbox" name="country" value="Bangladesh">Bangladesh</label>
</div>

Mohammad Ali Rony
- 4,695
- 3
- 19
- 33
-
how to use above link in my code ? based on the output how to use country code to display/hide checkbox ? – vreddy Mar 27 '18 at 19:51
-