Populate Native HTML Datalist with AJAX

Size
2,763 Kb
Views
54,648

How do I make an populate native html datalist with ajax?

A demo of how to populate a native HTML datalist field with AJAX. Using Rotten Tomatoes' Movie Search API. See where datalist is supported. What is a populate native html datalist with ajax? How do you make a populate native html datalist with ajax? This script and codes were developed by Joshua P. Larson on 23 July 2022, Saturday.

Populate Native HTML Datalist with AJAX Previews

Populate Native HTML Datalist with AJAX - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Populate Native HTML Datalist with AJAX</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <input type="text" list="movies" data-movie-search placeholder="Start searching for a movie..." />
<datalist id="movies" data-movie-list></datalist> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Populate Native HTML Datalist with AJAX - Script Codes CSS Codes

body { background: #336699;
}
[data-movie-search] { border: 2px solid #efefef; font-size: 1.5em; display: block; width: 80%; padding: 1em; margin: 3rem auto;
}

Populate Native HTML Datalist with AJAX - Script Codes JS Codes

'use strict';
var MOVIE_SEARCH = { rtApiKey: '', rtApiEndpoint: '', delay: 0, timer: 0, input: {}, list: {}, init: function() { // Define elements this.input = $('[data-movie-search'); this.list = $('[data-movie-list]'); // Set delay this.delay = 300; // 300 milliseconds // Set Rotten Tomatoes data this.rtApiKey = 'jpe83dcf7beyxqn6gxwm2cwa'; this.rtApiEndpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json'; // Add event listeners this.input.on('keyup', this.setTimer.bind(this)); }, setTimer: function() { if ( this.timer ) { clearTimeout(this.timer); } this.timer = setTimeout( this.processQuery.bind(this), this.delay ); }, processQuery: function() { var query = this.input.val(); if ( ! query ) { return false; } $.ajax({ dataType: 'jsonp', url: this.rtApiEndpoint, data: { apikey: this.rtApiKey, q: query }, success: this.updateMovieList.bind(this) }); }, updateMovieList: function( response ) { if ( 'object' === typeof( response.movies ) ) { var movies = response.movies; // Clear the list this.list.html(''); $.each( movies, function(idx, movie) { this.list.append('<option>' + movie.title + '</option>'); }.bind(this)); } }
};
$(document).ready(function() { MOVIE_SEARCH.init();
});
Populate Native HTML Datalist with AJAX - Script Codes
Populate Native HTML Datalist with AJAX - Script Codes
Home Page Home
Developer Joshua P. Larson
Username jplhomer
Uploaded July 23, 2022
Rating 3
Size 2,763 Kb
Views 54,648
Do you need developer help for Populate Native HTML Datalist with AJAX?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Joshua P. Larson (jplhomer) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!