I am using the Chuck Norris joke generator API. I was wondering if it would be possible in this situation to change part of the string returned 'Chuck Norris' to something else. I've tried a few different ideas and I'm really stuck.
E.g instead of 'Chuck Norris built the hospital he was born in.' the API could return 'Dalai Lama built the hospital he was born in.' instead.
document.querySelector('.get-jokes').addEventListener('click', getJokes);
function getJokes(e) {
const number = document.querySelector('input[type="number"]').value;
const xhr = new XMLHttpRequest();
xhr.open('GET', `http://api.icndb.com/jokes/random/${number}`, true);
xhr.onload = function() {
if (this.status === 200) {
const response = JSON.parse(this.responseText);
console.log(response);
let output = '';
if (response.type === 'success') {
response.value.forEach(function(getjoke) {
output += `<li>${getjoke.joke}</li>`;
});
} else {
output += '<li>Something went wrong</li>';
}
document.querySelector('.jokes').innerHTML = output;
}
};
xhr.send();
e.preventDefault();
}
<div class="container">
<h2>Chuck Norris Joke Generator</h2>
<form>
<div>
<label for="number">Number of jokes</label>
<input type="number" id="number">
</div>
<div>
<button class="get-jokes">Get Jokes</button>
</div>
</form>
<ul class="jokes"></ul>
</div>