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

266
Views
I am trying to display a quote after typing author name in the textbox but for some reason it is not displaying anything. How should I fix this?

I am getting undefined when I type the author name in the text box and press the button to display the quote. It seems like my button and textbox are not linked together. How can I fix this?

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Quotes</title>
</head>
<body>

<label for="getQuotes">Find Quotes (Type Author Name)</label><br>
<input type = "text" id="getQuotes" name="getQuotes" placeholder="Search" style="margin:10px" size="50"/><br />
<button id="FetchQuotes" onclick="getQuote()"  style="margin:10px">Fetch Quotes</button>

<p id="quotes"></p>

<p id="author"></p>

<script>
    async function getQuote() {
        //const author = Boolean(false);
        let url = 'https://jaw1042-motivate.azurewebsites.net/quote';
        let author = document.getElementById('getQuotes').value;
        if(author) {
            url = 'https://jaw1042-motivate.azurewebsites.net/quote?author= ';
            console.log(url + author);
        } else {
            console.log(url);
        }
        fetch(url)
            .then(async (response) => {
                if (response.ok) {
                    console.log("Response code: " + response.status);
                } else if (response.status === 400) {
                    console.log("Unable to find any quotes by specified author: " + response.status);
                } else {
                    console.log("No quotes have been loaded: " + response.status);
                }
                const val = await response.json();
                console.log(val);
            }).then(data => {
            document.getElementById('quotes').value = data;
            document.getElementById('author').value = data;
            console.log(data);
            alert(data);
        });
    }
</script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

your then functions are not correct in the direct result of the fetchAPI you can receive data and to use it you need to run .json() or .text() on it, you can't simply use that result or return it's value ( plus when you use return statement all your next codes will be unreachable) after that you should not assign something to your data variable because it just has new Data fetched from backend, by assigning new value to data you're about to ruin new data

here is how your js should look

     function getQuote() {
       fetch("https://krv1022-motivate.azurewebsites.net/quote")         
         .then( res => res.text() )
         .then( data => {
             document.querySelector(".quote").value = data;
         }
       );
    }

I also provided a fiddle for it but it can't receive data because either your URL is not correct or there is CORS issues

==============================================

one thing that I just noticed, you are receiving Author's name from end user but you are not about to send it to backend!

so perhaps this code is more complete, I assume that you want to send data using GET method and backend wants the name of author to be named getQuotes

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!