0

i'm new in JavaScript.

I have an array with some values and I'd like to show each array values within a list.

This is my script

<script>
var fruits = "apple,banana,watermelon,coconut";
var plits = fruits.split(',');
function a(){
  for(i=0; i<plits.length; i++){
    document.getElementById('output').innerHTML = "<li>" + plits[i] + " fruit</li>";
    //document.write("<li>" + plits[i] + " fruit</li>");
  }
}
</script>

<button onclick="a()">show fruits</button>
<div id='output'></div>

Whenever i run the code, it only shows the latest value of the array.

Likewise when i use the commented script above, the page changed and it turns infinity loading.

How can i show the array values within the #output?

2
  • UL should be the parent of li elements. Also you have to concatenate the innerHTML like document.getElementById('output').innerHTML += "<div>" + plits[i] + " fruit</div>"; Commented Apr 4, 2016 at 12:12
  • 2
    That is because you write all the values to the same spot. Commented Apr 4, 2016 at 12:13

5 Answers 5

3

Better use document.createElement and Node.appendChild for it.

function a() {
    var ul = document.createElement('ul'),
        li, i;

    for (i = 0; i < plits.length; i++) {
        li = document.createElement('li');
        li.innerHTML = plits[i];
        ul.appendChild(li);
    }
    document.getElementById('output').appendChild(ul);
    a = function () {};
}

var fruits = "apple,banana,watermelon,coconut",
    plits = fruits.split(',');
<button onclick="a()">show fruits</button>
<div id='output'></div>

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

4 Comments

Thanks, it helps !
#Impressed with your otherwise thinking ;)
@RayonDabre, what do you mean?
I have been reading your answers every now and then and I always find it different from others :)
1

replace

document.getElementById('output').innerHTML = "<li>" + plits[i] + " fruit</li>";

with

document.getElementById('output').innerHTML += "<li>" + plits[i] + " fruit</li>";

Complete function

function a(){
  var html = "<ul>";
  for(i=0; i<plits.length; i++)
  {
    html += "<li>" + plits[i] + " fruit</li>";
  }
  html += "</ul>";
  document.getElementById('output').innerHTML = html;
}

For ensuring that a() is not invokable again, set it to null

function a(){
  var html = "<ul>";
  for(i=0; i<plits.length; i++)
  {
    html += "<li>" + plits[i] + " fruit</li>";
  }
  html += "</ul>";
  document.getElementById('output').innerHTML = html;
  a = null;// or a = function(){}
}

8 Comments

@Rayon Also li cannot be a child of div
@RajaprabhuAravindasamy fixed it
@RayonDabre true, took me more time than I thought ;)
thanks, it works! But, can you tell me how to make the function only works for 1 time?
@ArifZoel IIFE is the thing that you must be looking for.
|
0

What you are doing is replacing the innerHTML of the div in for loop.. But what you really needs to do is to append string in every itteration.

<script>
var fruits = "apple,banana,watermelon,coconut";
var plits = fruits.split(',');
function a(){
  var message = "<ul>";
  for(i=0; i<plits.length; i++){
     message += '<li>'+plits[i] + " fruit</li>";
  }
  message += '</ul>'
  document.getElementById('output').innerHTML = message;
}
</script>

<button onclick="a()">show fruits</button>
<div id='output'></div>

Comments

0
            <!DOCTYPE html>
            <html>
            <head>
                <title></title>
            </head>
            <body>
            <script>
            var fruits = "apple,banana,watermelon,coconut";
            var plits = fruits.split(',');
            var content = '';
            function a(){
              for(i=0; i<plits.length; i++){
                content = content.concat("<li>" + plits[i] + " fruit</li>");  
              }
              document.getElementById('output').innerHTML = content;
            }
            </script>

            <button onclick="a()">show fruits</button>
            <div id='output'></div>
            </body>
            </html>

Comments

0

Check out this fiddle:

https://jsfiddle.net/0my1xx7c/

<button id='button'>show fruits</button>
<ul id='output'></ul>


<script>
var fruits = "apple,banana,watermelon,coconut";
var plits = fruits.split(',');
function a(){
  for(i=0; i<plits.length; i++){
    var li = document.createElement('li');
    li.textContent = plits[i] + ' ' + 'fruit';
    document.getElementById('output').appendChild(li);
  }
}

document.getElementById('button').addEventListener('click', a, false);
</script>

Remarks:

  • Don't append li items to a div, but a ul or ol element
  • Instead of writing , add an id to the element and attach an event listener to it that executes the function after the button has been clicked
  • Create and append elements instead of using innerHTML

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.