Andrei Simedre
Andrei Simedre

Reputation: 45

Want to add checkboxes to dynamically created li

This is my code. This adds li dynamically to the with id "list". How can I add to every li a checkbox?

  $("#add").on("click", function(e){
    $("#list").append("<li>" + $("#prenume").val() + " " + $("#nume").val() + "</li>");
    return false;
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<form>
      Prenume
      <input type="text" id="prenume"></input>
      Nume
      <input type="text" id="nume"></input>
</form>

<button id="add">Add</button>

<ul id="list">
</ul>

Upvotes: 0

Views: 1192

Answers (1)

Wowsk
Wowsk

Reputation: 3675

After you append the li in your JavaScript, add an input element with the type checkbox.

$("#add").on("click", function(e){
    $("#list").append("<li><input type='checkbox'>" + $("#prenume").val() + " " + $("#nume").val() + "</li>");
    return false;
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<form>
      Prenume
      <input type="text" id="prenume"></input>
      Nume
      <input type="text" id="nume"></input>
</form>

<button id="add">Add</button>

<ul id="list">
</ul>

Upvotes: 2

Related Questions