-
-
Save codecademydev/34ae11c210c0c6ba6ff45021463c182a to your computer and use it in GitHub Desktop.
Codecademy export
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
// Information to reach API | |
const apiKey = '<Your API Key>'; | |
const url = 'https://api.rebrandly.com/v1/links'; | |
// Some page elements | |
const inputField = document.querySelector('#input'); | |
const shortenButton = document.querySelector('#shorten'); | |
const responseField = document.querySelector('#responseField'); | |
// Asynchronous functions | |
const shortenUrl = () => { | |
const urlToShorten = inputField.value; | |
const data = JSON.stringify({destination: urlToShorten}); | |
fetch(url, { | |
method: 'POST', | |
headers: { | |
'Content-type': 'application/json', | |
'apikey': apiKey | |
}, | |
body: data | |
}) | |
} | |
// Clear page and call Asynchronous functions | |
const displayShortUrl = (event) => { | |
event.preventDefault(); | |
while(responseField.firstChild){ | |
responseField.removeChild(responseField.firstChild); | |
} | |
shortenUrl(); | |
} | |
shortenButton.addEventListener('click', displayShortUrl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment