A Pen by Captain Anonymous on CodePen.
Created
August 10, 2017 07:48
-
-
Save anonymous/c3de70c6f5fae04b0620cd3b3551d52c to your computer and use it in GitHub Desktop.
RGOqQY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<header> | |
<h1>JSON and AJAX</h1> | |
<button id="btn">Fetch Info for 3 New Animals</button> | |
</header> | |
<div id="animal-info"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pageCounter = 1; | |
var animalContainer = document.getElementById("animal-info"); | |
var btn = document.getElementById("btn"); | |
btn.addEventListener("click", function() { | |
var ourRequest = new XMLHttpRequest(); | |
ourRequest.open('GET', 'https://learnwebcode.github.io/json-example/animals-' + pageCounter + '.json'); | |
ourRequest.onload = function() { | |
if (ourRequest.status >= 200 && ourRequest.status < 400) { | |
var ourData = JSON.parse(ourRequest.responseText); | |
renderHTML(ourData); | |
} else { | |
console.log("We connected to the server, but it returned an error."); | |
} | |
}; | |
ourRequest.onerror = function() { | |
console.log("Connection error"); | |
}; | |
ourRequest.send(); | |
pageCounter++; | |
if (pageCounter > 3) { | |
btn.classList.add("hide-me"); | |
} | |
}); | |
function renderHTML(data) { | |
var htmlString = ""; | |
for (i = 0; i < data.length; i++) { | |
htmlString += "<p>" + data[i].name + " is a " + data[i].species + " that likes to eat "; | |
for (ii = 0; ii < data[i].foods.likes.length; ii++) { | |
if (ii == 0) { | |
htmlString += data[i].foods.likes[ii]; | |
} else { | |
htmlString += " and " + data[i].foods.likes[ii]; | |
} | |
} | |
htmlString += ' and dislikes '; | |
for (ii = 0; ii < data[i].foods.dislikes.length; ii++) { | |
if (ii == 0) { | |
htmlString += data[i].foods.dislikes[ii]; | |
} else { | |
htmlString += " and " + data[i].foods.dislikes[ii]; | |
} | |
} | |
htmlString += '.</p>'; | |
} | |
animalContainer.insertAdjacentHTML('beforeend', htmlString); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body { | |
padding: 0; | |
margin: 0; | |
} | |
.hide-me { | |
visibility: hidden; | |
opacity: 0; | |
transform: scale(.75); | |
} | |
h1 { | |
margin-top: 0; | |
font-size: 2.4em; | |
font-weight: normal; | |
display: inline-block; | |
} | |
body { | |
font-family: Helvetica, sans-serif; | |
padding: 50px 10%; | |
} | |
button { | |
background-color: #046380; | |
color: #FFF; | |
border: none; | |
padding: 10px 15px; | |
font-size: 15px; | |
border-radius: 4px; | |
cursor: pointer; | |
outline: none; | |
box-shadow: 2px 2px 0 #034154; | |
margin-bottom: 10px; | |
margin-left: 18px; | |
transition: opacity .4s ease-out, transform .4s ease-out, visibility .4s ease-out; | |
position: relative; | |
top: -10px; | |
} | |
button:hover { | |
background-color: #034F66; | |
} | |
button:active { | |
background-color: #034154; | |
box-shadow: none; | |
position: relative; | |
top: -8px; | |
left: 2px; | |
} | |
p { | |
padding: 4px 0 2px 8px; | |
line-height: 1.7; | |
border-bottom: 1px dotted #DDD; | |
list-style: none; | |
margin: 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment