133

How do I add a list element to an existing ul using a function from an onclick? I need it to add to this type of list ...

<ul id="list">
<li id="element1">One</li>
<li id="element2">Two</li>
<li id="element3">Three</li>
</ul> 

... another list item with the id "element4" and text "Four" under that. I tried this function but it doesn't work...

function function1() {
  var ul = document.getElementById("list");
  var li = document.createElement("li");
  li.appendChild(document.createTextNode("Element 4"));
}

I don't know JQuery so Javascript only please. Thank you!!

6 Answers 6

253

You have not appended your li as a child to your ul element

Try this

function function1() {
  var ul = document.getElementById("list");
  var li = document.createElement("li");
  li.appendChild(document.createTextNode("Four"));
  ul.appendChild(li);
}

If you need to set the id , you can do so by

li.setAttribute("id", "element4");

Which turns the function into

function function1() {
  var ul = document.getElementById("list");
  var li = document.createElement("li");
  li.appendChild(document.createTextNode("Four"));
  li.setAttribute("id", "element4"); // added line
  ul.appendChild(li);
  alert(li.id);
}
Sign up to request clarification or add additional context in comments.

Comments

19

You were almost there:

You just need to append the li to ul and voila!

So just add

ul.appendChild(li);

to the end of your function so the end function will be like this:

function function1() {
  var ul = document.getElementById("list");
  var li = document.createElement("li");
  li.appendChild(document.createTextNode("Element 4"));
  ul.appendChild(li);
}

Comments

14

First you have to create a li(with id and value as you required) then add it to your ul.

Javascript ::

addAnother = function() {
    var ul = document.getElementById("list");
    var li = document.createElement("li");
    var children = ul.children.length + 1
    li.setAttribute("id", "element"+children)
    li.appendChild(document.createTextNode("Element "+children));
    ul.appendChild(li)
}

Check this example that add li element to ul.

Comments

9

Just use innerHTML:

function function1() {
  ul.innerHTML += `<li> four </li>`;
}

1 Comment

innerHtml is slower and it's XSS - Cross Site Scripting attack prone
0

Object.assign can be used to set the textContent (and the id) of the <li> element directly, and the return value can be passed to Element#append to add the list item to the unordered list.

ul.append(Object.assign(document.createElement('li'), {textContent: 'Element 4'}));

Full example:

const ul = document.getElementById('list');
document.querySelector('button').addEventListener('click', e => {
  ul.append(Object.assign(document.createElement('li'), 
              {textContent: 'Four', id: 'element4'}));
}, {once: true});
<ul id="list">
  <li id="element1">One</li>
  <li id="element2">Two</li>
  <li id="element3">Three</li>
</ul>
<button>Add item</button>

Comments

0

This is more easy and straightforward. We don't need to use document.createTextNode unless if we need more flexibility.
Simpler and more practical way is using textContent, which is also a recommended way in JavaScript.

document.getElementById('addItemBtn').addEventListener('click', function() {
    // Get the input field and the list
    const inputField = document.getElementById('itemInput');
    const itemList = document.getElementById('itemList');

    // Get the value from the input field
    const itemValue = inputField.value.trim();

    // Check if the input is not empty
    if (itemValue !== '') {
        // Create a new list item element
        const newItem = document.createElement('li');
        newItem.textContent = itemValue;

        // Add the new item to the list
        itemList.appendChild(newItem);

        // Clear the input field for new input
        inputField.value = '';
    }
});

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.