3

I have a javascript function which generates a ul list based on an array being passed in using a similar approach to this - Create a <ul> and fill it based on a passed array

However, when I do the following...

document.getElementById("list").innerHTML = generateListFromArray(array);

All that gets printed is

[object HTMLUListElement]

Can anyone tell me how to print the contents into the div as HTML?

3
  • Instead return a html string from the function. Commented Jul 7, 2016 at 8:15
  • Return string from function and finally it will be like that document.getElementById("list").innerHTML = "<ul><li>List1</li><li>List2</li><li>List3</li> </ul>" Commented Jul 7, 2016 at 8:19
  • It would be very helpful to know what the id="list" element is. Commented Jul 7, 2016 at 8:31

4 Answers 4

6

You're creating a proper UL element (HTMLUListElement), which is great. You can use that directly by simply appending it to your target:

document.getElementById("list").appendChild(generateListFromArray(array));

If the target already contains content you want to replace (rather than add to), you can clear the target element first:

var list = document.getElementById("list");     // Get the target element
list.innerHTML = "";                            // Remove previous content
list.appendChild(generateListFromArray(array)); // Append your generated UL

There's simply no reason, at all, to convert the element you created to markup first (by using .innerHTML or .outerHTML on the return value of generateListFromArray).

If list is also a ul and you want to replace it, you can do that with insertBefore and removeChild:

var list = document.getElementById("list");     // Get the target element
var parent = list.parentNode;                   // Get its parent
var newList = generateListFromArray(array);     // Get the new one
parent.insertBefore(
    newList,                                    // Insert the new list...
    list                                        // ...before the old one
);
parent.removeChild(list);                       // Remove the old
newList.id = "list";                            // Give the new list the ID the
                                                // old one had
Sign up to request clarification or add additional context in comments.

Comments

3

Either use innerHTML or outerHTML:

document.getElementById("list").innerHTML = generateListFromArray(array).innerHTML;

Use the innerHTML if you already have the list as <ul>.

Comments

1

You might like to use appendChild() method:

document.getElementById("list").appendChild(generateListFromArray(array)));  

3 Comments

No, this is not what OP wants.
@dfsq yup got it from your answer.
But you can clean innerHTML before appending, then it should be similar more or less.
1

generateListFromArray returns HTMLUListElement. The simplest solution is to write its outerHTML:

document.getElementById("list").innerHTML = generateListFromArray(array).outerHTML;

However, if #list element in HTML is already a UL then you don't want to have extra <ul></ul> (markup will be invalid). In this case you would need to use innerHTML:

document.getElementById("list").innerHTML = generateListFromArray(array).innerHTML;

3 Comments

Oops... Sorry dfsq! :)
@PraveenKumar I just realized later that list might imply that it's a UL, so updated the answer. Don't be so paranoid, man :)
@dfsq I know... I apologized sorry! :(

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.