jQuery API Show Country Details


Code

<!DOCTYPE html>
<html>
<head>
<title>Web Application</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<style type="text/css">
*{
font-size: 100%;
font-family: arial;
padding: 0px;
}
</style>
</head>
<body>
<center>
<h1>jQuery API Show Country Details</h1>
<select id="choose"></select>
<hr/>
<p id="blog"></p>
</center>

<script type="text/javascript">
$.ajax({
method: "get",
url: "https://restcountries.eu/rest/v2/all",
type: 'json',
}).done(function( msg ) {
var data = "";
for (var i = 0; i < msg.length; i++) {
data+="<option value="+msg[i].alpha2Code+">"+msg[i].name+"</option>";
}
$('#choose').html(data);
});

$('#choose').on('change',function(){
var code = $('#choose').val();
var url = 'https://restcountries.eu/rest/v2/alpha/'+code;
$.ajax({
method: "get",
url: url,
type: 'json',
})
.done(function( msg ) {
var blog ='';
blog+='<b>name : &nbsp; </b>'+msg.name+'<br/>';
blog+='<b>nativeName : &nbsp; </b>'+msg.nativeName+'<br/>';
blog+='<b>numericCode : &nbsp; </b>'+msg.numericCode+'<br/>';
blog+='<b>population : &nbsp; </b>'+msg.population+'<br/>';
blog+='<b>region : &nbsp; </b>'+msg.region+'<br/>';
$('#blog').html(blog);
});
})
</script>
</body>
</html>

Demo:

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment