Condidtionally Appending Clicked Element to Another Element
by Luke
HTML
<ul id="selecteds">
</ul>
<ul id="list">
<li>asdf</li>
<li>qwer</li>
<li>zxcv</li>
</ul>
JavaScript
$("#list li").on("click", function(event) {
var t = $(this).text(); // the clicked text
var exists = false; // is it already selected?
$('#selecteds li').each(function() {
if ($(this).text() == t) {
exists = true; // found a match
return false; // stop looking
}
});
// no match? add one
if (! exists) {
$('<li>').text(t).appendTo('#selecteds');
}
});