I was writing a code to append a card with a new header every time a button is pressed also the header is dynamically providing by user. the problem is i cant make the function work properly for more than one card. I know the problem is that every card has the same class hence the new header will be appended to each card.I also want to add different user input lists to these cards.
$(".add").click(function(){
let list = $("#userInp").val()
$("#mainCard").append('<div class="card col-xl-3" ><div class="card-header" id ="head" ></div><div class="card body" ><ul></ul></div></div>')
$("#mainCard").find("#head").append('<button type = "checkbox" class ="form-check-input" id = "checkbox01"></button>'+list+'')
})
This is my code.And thanks for helping me.
3
Answers
You can check existing length of the cards like below
Consider the following.
You can skip the head id all together. Work with a document fragment and you are contained to working with just what you are adding.
NOTE the code below is untested and may need some tweaking.