-2

I have a dynamic select function and when a click is performed I want to prepend only once per div within.

So far my code just prepends every time. I just can't figure it out. Any advice is appreciated, thank you.

var selGroup = $(".selected-results > .results-group");

if (!$('.selected-results > .results-group > .results-category').length) {
  selGroup.prepend('<li class="results-category" data-class="'
    + category +'">'+ category +'</li>');
  return true;
} else {
}
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
HeyImArt
  • 498
  • 3
  • 9
  • 24
  • Your `if` statement should prevent multiple appends, so logic would dictate that the condition is flawed. We cannot debug this without seeing a more representative example, though. – Rory McCrossan Nov 06 '19 at 10:26
  • I'll try and get a better example up, sorry – HeyImArt Nov 06 '19 at 10:30

1 Answers1

-1

I figured out my resolution.

$(".selected-results > .results-group").each(function() {

        if ($(this).children('.results-category').length){
          return false;
        } 
        else {
          $(this).prepend('<li class="results-category" data-class="'+ category +'">'+ category +'<span class="show-more"></span></li>');
        }

      });
HeyImArt
  • 498
  • 3
  • 9
  • 24