Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove element from a cached jquery selection

This should'nt be that difficult but for some reason i'm having trouble with this.

I want to remove the first li from a cached ul:

var $selection = jQuery('.list').filter('.exclude');

.

<ul class="list">
  <li class="exclude" >Do not Include</li>
  <li>2</li>
  <li>3</li>
</ul>

How do I capture the entire ul object without the first child li element in it?

Thanks

EDIT: The resulting jquery object i'm looking for is:

<ul class="list">
  <li>2</li>
  <li>3</li>
</ul>

Not this:

  <li>2</li>
  <li>3</li>
like image 304
Jose Browne Avatar asked Dec 26 '22 22:12

Jose Browne


2 Answers

jsBin demo

var $selection = jQuery('.list li').not('.exclude');

(You were close:) Using the .not() (or :not) selector and $('.list li') elements collection (to be filtered)


$('.list li').remove('.exclude');

demo

http://api.jquery.com/not-selector/
http://api.jquery.com/not

like image 77
Roko C. Buljan Avatar answered Jan 18 '23 10:01

Roko C. Buljan


http://jsfiddle.net/9NtLF =)

var list = $("ul.list").clone();
list.find("li.exclude").remove();
like image 29
see613 Avatar answered Jan 18 '23 10:01

see613