0

I'm selecting data from an API which i then make into Objects with document.createElement but how can i style them?

Here's my Js code:

  fetch('https://randomuser.me/api/?results=5&nat=us').then(response =>{
        return response.json();
    }).then(responseJson=>{
        responseJson.results
        console.log(responseJson);

        for(const user of responseJson.results){
            const img = document.createElement ("IMG");
            img.innertext = user.picture.medium;
            img.setAttribute("src", user.picture.medium)
            img.setAttribute("width", "50");
            img.setAttribute("height", "50");
            img.setAttribute("alt", "");
            document.body.appendChild(img)

            const name = document.createElement("SPAN");
            name.innerText = user.name.first;
            document.body.appendChild(name);

            const phone = document.createElement("SPAN");
            phone.innerText = user.phone;
            document.body.appendChild(phone);

            console.log(user);
        }
    })

I've tried to refer to the name.innerText But that didn't work either. However i can refer to them all by calling, Example:

span {
 color: blue;
}

And when i inspect the code it shows that the span it creates has no id what so ever, May that be the problem?

2
  • 2
    Why don't you set a className and define that CSS class? Commented Sep 18, 2018 at 7:05
  • @connexo I've tried it but it's making it worse. I have 5 users and when i create a .className it only creates 1 user with the image only and leaves everything out. Commented Sep 18, 2018 at 7:11

3 Answers 3

3

To style these elements there is two ways:

  1. Add inline CSS to elements itself.

    phone.style.color = 'blue';

  2. Add some ID or class to element and then refer to them

    phone.className += "my-element";

    .my-element {color: blue;}

Sign up to request clarification or add additional context in comments.

1 Comment

phone.className += "my-element" should better be phone.className += " my-element", or even better phone.classList.add("my-element").
1

Use the className.add on the created element to assign a CSS class to it. I've modified your code to make all the names bold and the phone number blue and underlined.

fetch('https://randomuser.me/api/?results=5&nat=us').then(response =>{
    return response.json();
}).then(responseJson=>{
    responseJson.results
    console.log(responseJson);

    for(const user of responseJson.results){
        const img = document.createElement ("IMG");
        img.innertext = user.picture.medium;
        img.setAttribute("src", user.picture.medium)
        img.setAttribute("width", "50");
        img.setAttribute("height", "50");
        img.setAttribute("alt", "");
        document.body.appendChild(img)

        const name = document.createElement("SPAN");
        // Assign a CSS class name to the element.
        name.classList.add('user-name');
        name.innerText = user.name.first;
        document.body.appendChild(name);

        const phone = document.createElement("SPAN");
        phone.classList.add('phone-number');
        phone.innerText = user.phone;
        document.body.appendChild(phone);

        console.log(user);
    }
});
.user-name {
  font-weight: bold;
}

.phone-number {
  color: blue;
  text-decoration: underline;
}

1 Comment

Note that the img part could be simplified to const img = new Image(50, 50); img.src = user.picture.medium;.
0

Just add a className and create that CSS class in your CSS code:

// Javascript
let blue = document.createElement('p');
let orange = document.createElement('p');

blue.textContent = "blue text";
orange.textContent = "orange text";

blue.className = "blue";
orange.className = "orange";

app.appendChild(blue);
app.appendChild(orange);
/* CSS */
.blue { color: blue; }
.orange { color: orange; }
<!-- HTML -->
<div id="app"></div>

6 Comments

I've tried it but it's making it worse. I have 5 users and when i create a .className it only creates 1 user with the image only and leaves everything out.
Show your attempt then.
Could you give me a moment please? I'm trying @Thijs's answer.
Connexo i've tried stijn's answer and it is working. Your method would've worked aswell but apparently i had put the .className on the end of the span like: const name = document.createElement("SPAN").className = "name";
Yeah, you might be used to chaining tasks from jQuery, unfortunately in many cases this doesn't work with the native API in the same way. I tried to give a more generic answer independent of your current code problem, which is reduce to the core of what you are asking.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.