I have a favorites list that's populated by a click event. Even though the functionality is working the list items aren't being separated by line breaks:
Guide to Stuff.docxTemplate 1.docxTemplate 2.docxNext
Line.docxThen More.docx
I included <br/>
in .append()
but it didn't work. I've included line breaks with jQuery before so I'm not sure as to why it's not working now. Any thoughts?
function faveFunc(evt) {
var anchor = $($(evt.target).prev().find("a")[0]).clone();
switch($(".populate-faves").find("a:contains(" + $(anchor).text() + ")").length)
{
case 0:
$(".populate-faves").append(anchor);
break;
default:
$(".populate-faves > a:contains(" + $(anchor).text() + ")").remove();
break;
}
}; // ------------ faveFunc
function newList() {
let data = $($(evt.target).prev().find("a")[0]).html()
$(".populate-faves").html("");
$("#km-table-id tbody tr").each(function(i, el) {
let fave = $(el).find(".checkbox-class");
let itemText = $(el).find(data);
if($(fave).is(":checked")) {
// $(".populate-faves").append("<li>" + $(itemText).html() + "<br/>");
$(".populate-faves").append("<li> <br />");
}
});
console.log(newList);
}; // ----- newList()
...
<div id="myFave.hs-gc-header" class="faves-div">
<p style="font-weight:bold">My Favorites:</p>
<div class="populate-faves"></div> <!-- Where I want the list items to appear -->
</div>
...
function newList() {
let data = $(evt.target).prev().find("a").eq(0).html();
let outputList = $(".populate-faves .ul-faves");
$(".populate-faves").html("");
$("#km-table-id tbody tr").each(function(i, el) {
let fave = $(".checkbox-class", el);
let itemText = $(data, el);
if(fave.prop(":checked")) {
outputList.append("<li>" + itemText.html() + "</li>" + "<br/>");
// $(".populate-faves ul").append("<li> <br />");
}
});
// console.log(newList);
};
<div id="myFave.hs-gc-header" class="faves-div">
<p style="font-weight:bold">My Favorites:</p>
<div class="populate-faves">
<ul class="ul-faves"></ul>
</div>
</div>
Screencap
Added comments to the code and cleaned it up a bit. Added a ul
for it to append the elements to.
function newList() {
let data = $(evt.target).prev().find("a").eq(0).html(); // use eq() to avoid breaking out of the jQuery object
let outputList = $(".populate-faves .list"); // lookup the output list once
$(".populate-faves").html("");
$("#km-table-id tbody tr").each(function(i, el) {
// you can use the $(selector, context) version to avoid creating unnecessary jQuery objects
let fave = $(".checkbox-class", el);
let itemText = $(data, el);
// if you already have the element, use prop('checked') instead of is(':checked') to access the boolean property on the element
if(fave.prop("checked")) {
//append the li to the list, properly closing it
outputList.append("<li>" + itemText.html() + "</li>");
}
});
};
<div id="myFave.hs-gc-header" class="faves-div">
<p style="font-weight:bold">My Favorites:</p>
<div class="populate-faves">
<ul class="list"></ul> <!-- CREATED unorder list FOR OUTPUT -->
</div>
</div>