Reputation:
<li> </li>
<li class="wordwrap"></li>
i want to remove li
on top level .ie li
without a class which resides just on top of li
with class .i don't want to remove any other li
? How can we do this with Jquery
Upvotes: 0
Views: 189
Reputation: 816452
If I understand you correctly, you want to remove the previous sibling li
element, if it has no class:
$('li[class]').prev('li:not([class])').remove();
This first selects all list elements with a class. Then selects all the previous sibling list elements that have no class and removes them.
Working Demo with this HTML:
<ul>
<li class="a">I will stay.</li>
<li>I will stay although I have no class</li>
<li>I will go away.</li>
<li class="a">I will stay.</li>
<li>I will go away.</li>
<li class="a">I will stay.</li>
</ul>
At least this is how I understood it based on your description. But it is a little vague, so if it is not what you want, please clarify.
Upvotes: 1
Reputation: 29267
You can use .prev() to get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.
If your <li class="i"></li>
is the only element with a className of i
, then you simply do:
$(".li").prev().remove()
However if there are many list elements with a className of i
then you need to match that particular one in your tree:
<ul id="cont">
<li></li>
<li class="li"></li> <!-- to reference this use $("#cont .li").get(0) -->
<li class="li"></li> <!-- to reference this use $("#cont .li").get(1) -->
</ul>
Upvotes: 0
Reputation: 1245
if your empty list item is always the first child you could do this:
$(".wordwrap").parent().find("li").first().remove();
Upvotes: 0