Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Using Axios to post a joke on click ( 1 at a time )

I could use some help. I have a button and on click, it generates a joke using dad jokes API and Axios. However, I can't figure out how to get rid of the old joke when I click on the button again. Here is my code. Thanks

let button = document.querySelector('button') button.addEventListener("click", getJoke)

 function getJoke() {
const p = axios
.get('https://icanhazdadjoke.com/', { headers: { "Accept": "text/plain" },  
 })


 .then((response) => {
const joke = response.data

const jokeContainer = document.querySelector('.joke'); 

const blockquoteEl = document.createElement('blockquote');

blockquoteEl.append(joke);

jokeContainer.appendChild(blockquoteEl);
 })
  .catch((error) => {
const jokeContainer = document.querySelector('.joke');
jokeContainer.innerText = 'Joke not found :(';

  });

  }




 <div class="joke">
    <button>Click if you want a cringe joke</button>
  </div>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you need to store the incoming jokes in ana array to check the duplicates. try this

 function getJoke() {
const p = axios
.get('https://icanhazdadjoke.com/', { headers: { "Accept": "text/plain" },  
 })


 .then((response) => {
const joke = response.data

const jokesArr = [];

// stores joke history
jokesArr.push(joke);

// checkes the repeated joke in jokes history
const jokeCheck = (joke) => {
  const currentItemCount = jokesArr.filter(val => val=== joke).length;
  if(currentItemCount > 0) {
    p();
  }
  return joke;
}

const currentJoke = jokeCheck(joke);

const jokeContainer = document.querySelector('.joke'); 

const blockquoteEl = document.createElement('blockquote');

blockquoteEl.append(currentJoke);

jokeContainer.appendChild(blockquoteEl);
 })
  .catch((error) => {
const jokeContainer = document.querySelector('.joke');
jokeContainer.innerText = 'Joke not found :(';

  });

  }

about 4 years ago · Juan Pablo Isaza Report

0

What you should do is use the functions inherited by dom elements to delete de child node:

<body>
    <div class="joke">
    <button>Click if you want a cringe joke</button>
    </div>
    <script>
        let button = document.querySelector('button');
        button.addEventListener("click", getJoke);
        
        function getJoke(){
            const p = axios.get('https://icanhazdadjoke.com/', { headers: { "Accept": "text/plain" }
        }).then((response) => {

                const joke = response.data

                const jokeContainer = document.querySelector('.joke');

                jokeContainer.removeChild(jokeContainer.childNodes[2]);

                const blockquoteEl = document.createElement('blockquote');

                blockquoteEl.append(joke);

                jokeContainer.appendChild(blockquoteEl);
            }).catch((error) => {
                const jokeContainer = document.querySelector('.joke');
                jokeContainer.innerText = 'Joke not found :(';
            });
        }
    </script>
</body>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!