0

I want to add some items into the ul using JavaScript. Right now I am using createTextNode and createElement. Is there anyway to add this items using loop, to make code much shorter?

const ul = document.querySelector('ul');
        const listFeelTemp = document.createElement('li');
        const listTempMin = document.createElement('li');
        const listTempMax = document.createElement('li');
        const listPressure = document.createElement('li');
        const listHumidity = document.createElement('li');

        listFeelTemp.appendChild(document.createTextNode(`Feeeling temp is ${resFeelsTemp}°C`))
        listTempMin.appendChild(document.createTextNode(`Min ${resTempMin}°C`))
        listTempMax.appendChild(document.createTextNode(`Max ${resTempMax}°C`))
        listPressure.appendChild(document.createTextNode(`Pressure ${resPressure}HPa`))
        listHumidity.appendChild(document.createTextNode(`Humidity ${resHumidity}%`))

        ul.appendChild(listFeelTemp);
        ul.appendChild(listTempMin);
        ul.appendChild(listTempMax);
        ul.appendChild(listPressure);
        ul.appendChild(listHumidity);

1
  • You'd probably need an array containing all the unique attributes of each field. Then loop over the array. Commented May 6, 2021 at 15:04

5 Answers 5

1

So the only changing thing in your code is the text: a good start will be to store those texts in an array.

Then, you can create a function that abstracts away all the logic in creating the <li> element and inserting it to the <ul> element. This is basically the concept of DRY: instead of repeating the same commands over and over again, abstract that logic into a function and call it iteratively.

Finally, loop through your array and invoke the function:

const ul = document.querySelector('ul');

// Abstract element & text node creation to a function
function createAndAppendListItem(text) {
    const el = document.createElement('li');
    el.appendChild(document.createTextNode(text);
    ul.appendChild(el);
}

const texts = [
    `Feeeling temp is ${resFeelsTemp}°C`,
    `Min ${resTempMin}°C`,
    `Max ${resTempMax}°C`,
    `Pressure ${resPressure}HPa`,
    `Humidity ${resHumidity}%`
]

texts.forEach(text => createAndAppendListItem(text));

Of course, if your data structure is going to be more complicated, then considering storing data in the form of an array of objects instead of an array of strings.

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

Comments

0

You can do this simple method

const array = [`Feeeling temp is ${resFeelsTemp}°C`, `Min ${resTempMin}°C`, `Max ${resTempMax}°C`, `Pressure ${resPressure}HPa`, `Humidity ${resHumidity}%` ]

const ul = document.querySelector('ul');
array.forEach((value) =>{
  const li = document.createElement('li');
  li.innerText = value
  ul.appendChild(li)
})

1 Comment

This is exactly solution that i tried to develop. Thanks
0

How about this?

    const textValues = [
      `Feeeling temp is ${resFeelsTemp}°C`,
      `Min ${resTempMin}°C`,
      `Max ${resTempMax}°C`,
      `Pressure ${resPressure}HPa`,
      `Humidity ${resHumidity}%`
    ];
    
    const ul = document.querySelector('ul');

    for (const textValue of textValues) {
      const li = document.createElement('li');
      li.appendChild(document.createTextNode(textValue));
      ul.appendChild(li);
    }

Comments

0

I would make a dedicated function which creates the element and append it to the ul.

Another suggestion is to keep an array of template literals which holds the string templates and replace it with your hard coded temperature numbers.

const ul = document.createElement('ul');

const resFeelsTemp = 10;
const resTempMin = 12;
const resPressure = 13;
const resHumidity = 10;

const addUlToLi = (text) => {
     const li = document.createElement('li');
     li.appendChild(document.createTextNode(text));
     ul.appendChild(li);
}

const arr =[`Feeeling temp is ${resFeelsTemp}° C`, `Min ${resTempMin}°C`, `Pressure ${resPressure}HPa`, `Humidity ${resHumidity}%`];

arr.forEach(el => addUlToLi(el));


        

Comments

0

For the record: you can also use insertAdjacentHTML to create the elements

const ul = document.querySelector('ul');
const [feel, min, max, press, hum] = [22, 11, 25, 97.88, 45];
[`Feeling temp is ${feel}°C`,
 `Min ${min}°C`,
 `Max ${max}°C`,
 `Pressure ${press}HPa`,
 `Humidity ${hum}%`]
.forEach(txt => ul.insertAdjacentHTML("beforeend", `<li>${txt}</li>`));
<ul></ul>

Comments

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.