Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 26, 2023 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/34ae11c210c0c6ba6ff45021463c182a to your computer and use it in GitHub Desktop.
Save codecademydev/34ae11c210c0c6ba6ff45021463c182a to your computer and use it in GitHub Desktop.
Codecademy export
// 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