Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let inputS = document.querySelector('.filterPosts');
- let inputSearch = inputS.value.toUpperCase();
- let pList = document.querySelector('.postsList');
- let allLi = document.getElementsByTagName('li');
- let requestURL = 'Posts.json';
- let request = new XMLHttpRequest();
- request.open('GET', requestURL);
- request.responseType = 'json'; request.send();
- request.onload = function() {
- let posts = request.response;
- showPosts(posts);
- }
- function showPosts(jsonObj) {
- let posts = jsonObj['posts'];
- for (let i = 0; i < posts.length; i++) {
- let listContent = document.createElement('a');
- listContent.textContent = posts[i].name;
- listContent.href = posts[i].url;
- let liItem = document.createElement('li');
- liItem.appendChild(listContent);
- pList.appendChild(liItem);
- }
- }
- function search() {
- for (i = 0; i < allLi.length; i++) {
- txtValue = allLi[i].textContent || allLi[i].innerText;
- if (txtValue.toUpperCase().indexOf(inputSearch) > -1) {
- console.log('tem');
- allLi[i].style.display = '';
- } else {
- console.log('nao tem');
- allLi[i].style.display = 'none';
- }
- }
- }
- inputS.addEventListener('keyup',function(){
- search();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement