Automatically select visitor country in HTML form

In this tutorial we demonstrate how to automatically select visitor country in a form based on their IP address with client side Javascript code :

Make signup or order forms quicker and easier for your visitors and autofill the country field with their originating country.

When you add a data-auto-select-country attribute to dbip.js script tag, it will automatically fill visitor country for select elements that have a dbip-auto-select-country class :

Alternatively you can use the dbip.autoSelectCountry() method to autofill dynamically loaded forms :

dbip.autoSelectCountry() can optionally take a DOM selector as parameter and returns a Promise object :



Compatibility

API DB
Free Basic Core Extended IP to Country IP to City IP to Location IP to ISP IP to Location+ISP

Was this article helpful ? Share it with others by clicking the social media buttons !