1

I'm developing a new project that uses a lot of ul(unordered list) elements and some of this lists sharing the same values, so I want to make a Javascript function to automatize the process by passing a array for the function and returning the HTML for the page. Something like this:

<h1>Test Page</h1>
List fruits:
<script type="text/javascript" charset="utf-8">
    var fruits = ["Banana", "Apple", "Pear", "Strawberry", "Lemon"];
    do_list(fruits);
</script>

That should generate:

<h1>Test Page</h1>
List fruits:
<ul>
    <li>Banana</li>
    <li>Apple</li>
    <li>Pear</li>
    <li>Strawberry</li>
    <li>Lemon</li>
</ul>

How to make this?

3 Answers 3

3

Given a list of values, and the node you'd like to append, this will create li tags for every item in that list.

function do_list(arr, ulNode)
{
    for(var i = 0; i < arr.length; i++)
    {
        var node = document.createNode("li");
        node.nodeValue = arr[i];
        ulNode.appendNodes(node);
    }
}

Here's a better version though, it lets you create arbitrary node types -- you can pass in td as tagName and a tr as parentNode and it would populate that table row.

function do_list(arr, parentNode, tagName)
{
    if(!tagName)tagName="li";
    for(var i = 0; i < arr.length; i++)
    {
        var node = document.createNode(tagName);
        node.nodeValue = arr[i];
        parentNode.appendNodes(node);
    }
}

Use example:

<h1>Test Page</h1>
List fruits:
<ul id="list">
    <li>Banana</li>
    <li>Apple</li>
    <li>Pear</li>
    <li>Strawberry</li>
    <li>Lemon</li>
</ul>
<script type="text/javascript">
    do_list(["Banana", "Apple", "Pear", "Strawberry", "Lemon"], 
            document.getElementById("list"))
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

@Hippo While jQuery is awesome, it is often a sledgehammer when a scalpel is better.
Please use .nodeValue instead of .innerHTML to assign text content
The code won't work, even using your example it won't generate the list... :(
@NathanCampos on which browser?
0

Something like this?

<script type="text/javascript">
  function do_list(arr) {
    var out = '<ul>';
    for (var k=0; k < arr.length; k++) {
      out += '<li>' + arr[k] + '</li>';
    }
    out += '</ul>';
    return out;
  }
  var fruits = ["Banana", "Apple", "Pear", "Strawberry", "Lemon"];
  document.write(do_list(fruits));
</script>

2 Comments

Don't use document.write please. Especially since your not even bothering to .open or .close the document.
You don't need to call document.open when using document.write. If the document is closed, calling document.write will call document.open anyway. If the document isn't closed, you don't want to call document.open.
0

Try:

'<ul><li>' + fruitsArray.join('<li>') + '</ul>';

or as a function:

function arrayToList(array) {
  return '<ul><li>' + array.join('<li>') + '</ul>';
}

or if you really want closed LI tags (which are optional and not necessary in any browser):

function arrayToList(array) {
  return '<ul><li>' + array.join('</li><li>') + '</li></ul>';
}

If you want it inside some element:

function arrayToContainedList(container, array) {
  container.innerHTML = '<ul><li>' + array.join('</li><li>') + '</li></ul>';
}

And so on. I think the approach is clear.

5 Comments

the closing tag is missing on every <li>
This would result in a list of unclosed li's i.e( <li>banana<li>orange<li>pear<li>
Closing tags for LI elements are optional. But don't let that get in the way of a down vote. :-)
" if you really want closed" They are not optional. Browser parse shit HTML because that's how they make money. Write valid and semantic HTML please. or at least reference the W3C spec saying that <li> are optional in HTML4, HTML5 and XHTML
Raynos - closing tags for LI elements have always been optional, HTML5 doesn't change that. The example in the HTML 4.01 spec (an 11 year old spec) doesn't have closing tags. XHTML is irrelevant, no one actually serves it on the web. The vast majority of pages with XHTML DOCTYPEs are served as HTML (making the DOCTYPE irrelevant) and aren't valid XHTML anyway.

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.