I am calling data from API and listing the data by name. I want to search and add object by name from the list (using Add Fav Movie button) and store the entire data of that object in an array using native javascript.
I can do this in Angularjs , but less idea using native javascript.
function addListItem(title, listId) {
var ul = document.getElementById(listId);
var li = document.createElement("li");
li.className = 'list-group-item';
li.appendChild(document.createTextNode(title));
ul.appendChild(li);
}
function afterLoad() {
var data = JSON.parse(this.responseText);
var name = document.createElement('img');
const results = data.results;
// loop through items
results.forEach(item => {
addListItem(item.title, "items");
});
name.src = data.title;
document.body.appendChild(name);
}
function afterClick() {
// changed target to focus search
var terms = document.getElementById("search").value.split(' ').join('+');
var request = new XMLHttpRequest();
request.addEventListener('load', afterLoad);
request.open('GET', 'https://api.themoviedb.org/3/search/movie?api_key=8318c431b4fc8a2c4762bf2a52c351ee&query='+terms);
request.send();
}
button.addEventListener("click", afterClick);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"/>
<title>Get Movies</title>
</head>
<body>
<header id="main-header" class="bg-success text-white p-4 mb-3">
<div class="container">
<h1 id="header-title">Get Movies<span style="display:none">123</span></h1>
<input style="align:right" type="text" class="form-control mr-2" id="search">
<input type="submit" class="btn btn-dark" value="Search" id="button">
</div>
</header>
<div class="container">
<div id="main" class="card card-body">
<h2 class="title">Add Fav Movies</h2>
<form class="form-inline mb-3">
<input type="text" class="form-control mr-2">
<input type="submit" class="btn btn-dark" value="Submit">
</form>
<h2 class="title">Lists</h2>
<ul id="items" class="list-group">
</ul>
</div>
</div>
</body>
</html>
On response JSON I want to select object of movie (which I click on list ) under array key result. And append that particular movie object on another array. How can I do this ?
search(movie name).<li>as you can see. Now I want to type the particular movie name from that list onAdd Fav moviesinput and when I click add , the object related to that particular movie name must get stored in an array.Listsis empty. check your above code.