Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript get user from api

            fetch('https://jsonplaceholder.typicode.com/users')
        .then(function (response) {
            return response.json();
        })
        .then(function (data) {
            appendData(data);
        })
        .catch(function (err) {
            console.log('error: ' + err);
        });

    function appendData(data) {
        var mainContainer = document.getElementById("users");
        for (var i = 0; i < data.length; i++) {
            var li = document.createElement("li");
            li.innerHTML =  data[i].username;
            li.classList.add('item');
            li.dataset.userId = data[i].id;
            li.addEventListener('click', (event) => getPosts(event))
            mainContainer.appendChild(li);
        }

    }

    function cleanPosts() {
        var users = document.querySelectorAll('.item ul');
        for(var i = 0; i < users.length; i++) {
            if(users[i]) {
                users[i].style.display = 'none';
            }
        }
    }

    function getPosts(event) {

        var userId = event.target.dataset.userId;

        fetch(`https://jsonplaceholder.typicode.com/posts?userId=${userId}`)
            .then(response => response.json())
            .then(json => renderPosts(json, event.target))
    }

    function renderPosts(posts, target) {
        var postsList = target.childNodes[1];

        cleanPosts();

        if(postsList){
            postsList.style.display = 'block';
        } else {
            var list = document.createElement("ul");

            for (var i = 0; i < posts.length; i++) {

                var item = document.createElement("li");
                var liTitle = document.createElement("strong");
                var liBody = document.createElement("p");

                liTitle.innerHTML = posts[i].title;
                liBody.innerHTML = posts[i].body;

                item.appendChild(liTitle);
                item.appendChild(liBody);
                list.appendChild(item);
            }

            target.appendChild(list);
        }

    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: rating calculator formula javascript 
Javascript :: calculate time in seconds javascript angular 
Javascript :: graphql yoga access http headers 
Javascript :: on:click svelte arguments 
Javascript :: express req.body empty 
Javascript :: JavaScript Creating Symbol 
Javascript :: lexical environment in javascript 
Javascript :: #{} js 
Javascript :: mathjax arrow 
Javascript :: blob to pdf javascript 
Javascript :: Invalid prettier configuration file detected. See log for details. 
Javascript :: javascript access pushed element 
Javascript :: textinput onpress react native 
Javascript :: iis express gzip 
Javascript :: socket-client-io for reconnection in js or javascript 
Javascript :: push object into array javascript 
Javascript :: nodejs sqlite create db if not exists 
Javascript :: jest mock jwt-decode 
Javascript :: function hoisting in js 
Javascript :: what is shortest javascript program 
Javascript :: google places autocomplete react native 
Javascript :: antd: editable table example 
Javascript :: angular set attribute value dynamically 
Javascript :: correct way to push into state array 
Javascript :: Create buffers from strings using the Buffer.from() function. Like toString(), you can pass an encoding argument to Buffer.from(). 
Javascript :: length array 
Javascript :: input hook react 
Javascript :: is an Angular component, then verify that it is part of this module. 
Javascript :: moment.js get time from now 
Javascript :: remove elements from map javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =