Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
FREE SEO Check & Google Maps Check for Sri Lankan Businesses | Web Design, Website Price Rs 14,900 |
LankaSolve
Updated:
Yesterday at 7:01 PM
ඕනම රටකට සෙට් වෙන CV එකක්
ErMurazor
Updated:
Saturday at 9:50 AM
Hambantota
7 Acres Beachfront Land For Sell DownSouth , PerPerch Price 420 000
1024 Digital
Updated:
Oct 14, 2026
Baseus PI LITE INDOOR CAMERA
ErMurazor
Updated:
Oct 12, 2026
Ad icon
Colombo
Hard Disk For Sale
mazza
Updated:
Oct 10, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Talk!
Java Script help එකක්!!!
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="ishan0001" data-source="post: 21841000" data-attributes="member: 262672"><p><span style="font-size: 12px">Get country data from an API as JSON. Use it to populate the dropdown. Filter the JSON on button cllick and find the country code. <img src="/styles/default/xenforo/smilies/default/wink.gif" class="smilie" loading="lazy" alt=";)" title="Wink ;)" data-shortname=";)" /></span></p><p><span style="font-size: 12px">BTW this solution requires jQuery. If you need pure javascript, It's better to hardcode a country data json in the script than get it from an api</span></p><p><span style="font-size: 12px"></span></p><p>[PHP]</p><p><!DOCTYPE html></p><p><html></p><p> <head></p><p> <script</p><p> src="https://code.jquery.com/jquery-3.2.1.min.js"</p><p> integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="</p><p> crossorigin="anonymous"></p><p> </script></p><p></p><p> <script type="text/javascript"></p><p> </p><p> var countries;</p><p></p><p> function populate(){</p><p> var s1 = document.getElementById("slct1");</p><p></p><p> var selectedCountry = countries.filter(function(country) {</p><p> return country.name === s1.value;</p><p> });</p><p></p><p> if(selectedCountry[0] !== undefined)</p><p> alert('+' + selectedCountry[0].callingCodes[0]);</p><p> else</p><p> alert('Invalid country');</p><p> }</p><p></p><p> $(document).ready(function() {</p><p> $.getJSON("https://restcountries.eu/rest/v2/all", function(json){</p><p> countries = json;</p><p> $.each(countries, function(i, country) {</p><p> $('#slct1').append($('<option>').text(country.name).attr('value', country.name));</p><p> });</p><p> });</p><p> } </p><p> );</p><p> </p><p> </script></p><p> </head></p><p> <body></p><p> <h2>Select Country</h2></p><p> <hr /></p><p> <label for="slct1">Choose Country:</label></p><p> <select id="slct1" name="slct1"></select> </p><p> <button onclick="populate()"> Click me</button></p><p> <hr /></p><p> </body></p><p></html></p><p>[/PHP]<p style="text-align: center"><span style="color: #FFFFFF">Save</span></p> <p style="text-align: center"><span style="color: #FFFFFF">Save</span></p> <p style="text-align: center"><span style="color: #FFFFFF">Save</span></p></blockquote><p></p>
[QUOTE="ishan0001, post: 21841000, member: 262672"] [SIZE=3]Get country data from an API as JSON. Use it to populate the dropdown. Filter the JSON on button cllick and find the country code. ;) BTW this solution requires jQuery. If you need pure javascript, It's better to hardcode a country data json in the script than get it from an api [/SIZE] [PHP] <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"> </script> <script type="text/javascript"> var countries; function populate(){ var s1 = document.getElementById("slct1"); var selectedCountry = countries.filter(function(country) { return country.name === s1.value; }); if(selectedCountry[0] !== undefined) alert('+' + selectedCountry[0].callingCodes[0]); else alert('Invalid country'); } $(document).ready(function() { $.getJSON("https://restcountries.eu/rest/v2/all", function(json){ countries = json; $.each(countries, function(i, country) { $('#slct1').append($('<option>').text(country.name).attr('value', country.name)); }); }); } ); </script> </head> <body> <h2>Select Country</h2> <hr /> <label for="slct1">Choose Country:</label> <select id="slct1" name="slct1"></select> <button onclick="populate()"> Click me</button> <hr /> </body> </html> [/PHP][CENTER][COLOR=#FFFFFF]Save[/COLOR] [COLOR=#FFFFFF]Save[/COLOR] [COLOR=#FFFFFF]Save[/COLOR][/CENTER] [/QUOTE]
Insert quotes…
Verification
Asuwa dahayen wadi kalama keeyada?
Post reply
Top
Bottom