1

I am trying to create an application to display a certain number of list items based on the number input (i.e 3 = 3 list items). So far I have only been able to create a function that displays one list item with the onClick function. No matter what code I use, I cannot find a way to create multiple list items from one button click. Here is my current code, HTML:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
   <section>
       <header>Week 5 Assignment</header>
       <section>
           <label>Name: <input type="text" id="nameInput" value="Enter a Name"></label>
       </section>
    <section>
        <label>Num of Times: <input type="number" id="numInput" value="Enter Number"></label>
        <section>
           <ol id="nameOutput"></ol>
           <hr>
          <button onclick="displayName();">Display Name!</button> 
          <button>Reset</button> 
       </section>
    </section>
   </section>

    <script src="script.js"></script>
</body>
</html>

And my JS:

    var name = document.getElementById("nameInput").value;
    console.log(name);

    var num = document.getElementById("numInput").value;

    var list = document.getElementById("nameOutput");

    var item = document.createElement("li");

    item.innerText = name;
    list.append(item);
}

1 Answer 1

1

Javascript For loop should work for what you are trying to achieve

  <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
       <section>
           <header>Week 5 Assignment</header>
           <section>
               <label>Name: <input type="text" id="nameInput" value="Enter a Name"></label>
           </section>
        <section>
            <label>Num of Times: <input type="number" id="numInput" value="Enter Number"></label>
            <section>
               <ol id="nameOutput"></ol>
               <hr>
              <button onclick="displayName();">Display Name!</button> 
              <button>Reset</button> 
           </section>
        </section>
       </section>

        <script src="script.js"></script>
    </body>
    </html>
    <script>
    function displayName() {
        var name = document.getElementById("nameInput").value;

        var num = document.getElementById("numInput").value;

        var item = document.createElement("li");

        item.innerText = name;
        
        document.getElementById("nameOutput").innerHTML = '';

        for (i = 0; i < num; i++) {
        document.getElementById("nameOutput").innerHTML += '<li>'+name+'</li>';
        }
        
    }
    </script>

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

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.