Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Autocompletion

newdev

Coder
Hi to all,
I am new there. I am looking for help for an autocompletion script .
I tried to code it but ... it doesn't work 😢
Here are my codes
A simple HTML input with an ID
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Autocompletion </title>
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>

<label for="search">Search:</label>
<input type="text" id="search" name="search" autocomplete="off">

<div id="search-results"></div>

<script src="script.js"></script>
</body>
</html>

My Ajax / Jquery code
JavaScript:
$(document).ready(function() {
    $('#search').on('input', function() {
        var query = $(this).val();

        // I check if the input is not empty
        if (query.length > 0) {
            // Ajax request to JSONPlaceholder API
            $.ajax({
                url: 'https://jsonplaceholder.typicode.com/todos',
                type: 'GET',
                data: { title: query },
                success: function(data) {
                    // Display the results in the #search-results div
                    displayResults(data);
                }
            });
        } else {
            // Clear the #search-results div if the input is empty
            $('#search-results').empty();
        }
    });

    function displayResults(results) {
        // Display the results
        $('#search-results').empty();

        results.forEach(function(result) {
            $('#search-results').append('<div>' + result.title + '</div>');
        });
    }
});

Thanks for your precious possible help
Ludo
 
Solution
Your code is likely working as expected, but are you sure your ajax request is correct?

Sending a GET request in the browser with a given title value (https://jsonplaceholder.typicode.com/todos?title=foobar) returns an empty array.
If I try ?query=foobar rather than ?title=foobar, I get a response:
JSON:
[
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  },
  ...
]

Is this maybe what your ajax request should look like?
JavaScript:
$.ajax({
    url: 'https://jsonplaceholder.typicode.com/todos',
    type: 'GET',
    data: { query: query },
    success: function(data) {...
Your code is likely working as expected, but are you sure your ajax request is correct?

Sending a GET request in the browser with a given title value (https://jsonplaceholder.typicode.com/todos?title=foobar) returns an empty array.
If I try ?query=foobar rather than ?title=foobar, I get a response:
JSON:
[
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  },
  ...
]

Is this maybe what your ajax request should look like?
JavaScript:
$.ajax({
    url: 'https://jsonplaceholder.typicode.com/todos',
    type: 'GET',
    data: { query: query },
    success: function(data) {
        // Display the results in the #search-results div
        displayResults(data);
    }
});
 
Solution

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom