2

I have list:

<ul class="customList">
    <li data-featured="1">First item</li>
    <li data-featured="0">Another item</li>
    <li data-featured="0">Last item..</li>
</ul>

How can I insert my custom <li> after featured="1"? If all featured 0, then insert as first li.. Thanks in advance.

var self = this;
self.scope.find('.customList').prepend(myCustomItem); //this code inserts in begin, not after featured items..
$(myCustomItem).insertAfter('[data-featured="1"]'); //I've tried to build something.. but this fails

Sorry for my bad English

EDIT

myCustomItem.insertAfter('[data-featured="1"]'); looks like is working, but when there is any data-featured=1, then nothing happens. Any suggestions?

Laravel
  • 23
  • 5

4 Answers4

2

Use the jQuery attribute selector [attribute=value]:

$(" <li data-featured='222'>NEW ITEM</li>").insertAfter('.customList li[data-featured="1"]');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="customList">
    <li data-featured="1">First item</li>
    <li data-featured="0">Another item</li>
    <li data-featured="0">Last item..</li>
</ul>

Or if you want to insert it after the first li tag the, use this:

$(" <li data-featured='222'>NEW ITEM</li>").insertAfter('.customList li:first');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="customList">
    <li data-featured="1">First item</li>
    <li data-featured="0">Another item</li>
    <li data-featured="0">Last item..</li>
</ul>
Mehdi Bouzidi
  • 1,937
  • 3
  • 15
  • 31
1

You can first use the jQuery attribute selector [attribute=value]: to check if li with data-featured="1" is exist or not. If exist then use the jQuery method .insertAfter() to insert the new li after it and if not then insert new li at the first position using .insertBefore().

if($('.customList li[data-featured="1"]').length>0)
{
      $("<li data-featured='99'>NEW ITEM</li>").insertAfter('.customList li[data-featured="1"]');
}
else
{
      $("<li data-featured='99'>NEW ITEM</li>").insertBefore('.customList li:first');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="customList">
    <li data-featured="0">First item</li>
    <li data-featured="0">Another item</li>
    <li data-featured="0">Last item..</li>
</ul>
Mayur Patel
  • 1,741
  • 15
  • 32
0

First of all i would say don't use append() when the question is about insert after an li. Check below snippet, first check the condition for data-featured=1 available or not and then prepend() or insertAfter() based on that.

var flag = false;
$('.customList li').each(function() {
  if ($(this).data('featured') == '1') {
    flag = true;
  }
});
if (flag) {  
  $("<li data-featured='222'>NEW ITEM</li>").insertAfter('.customList li[data-featured="1"]');
} else {
  $('.customList').prepend(" <li data-featured='222'>NEW ITEM</li>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="customList">
  <li data-featured="1">First item</li>
  <li data-featured="0">Another item</li>
  <li data-featured="0">Last item..</li>
</ul>
RaJesh RiJo
  • 4,302
  • 4
  • 25
  • 46
0
newStatus.insertAfter('[data-featured="1"]');

Works fine but just have to check for the case when the item added is the first item or not. That can be easily done by if condition. something like this:

if($('li[data-featured="1"]') != null)

here is the example: Link

Mr.Bhanushali
  • 126
  • 12
  • if myCustomItem is array the you need to take a loop. iterate the array and then add it back to
      tag
    – Mr.Bhanushali Oct 03 '17 at 10:33
  • Nah, if I write `if($('li[data-featured="1"]') != null){alert('first');}else{alert('second');} And always I got alert first. Why?` – Laravel Oct 03 '17 at 10:33
  • if($('li[data-featured="1"]').val() != null) .... you need to take `.val()` like shown here. – Mr.Bhanushali Oct 03 '17 at 10:37