Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove elements greater or equal to a particular number using jquery?

Tags:

jquery

how to remove elements greater or equal to a particular number using jquery?

<div id="1" class="testList"><p>anything</p></div>
<div id="2" class="testList"><p>anything</p></div>
<div id="3" class="testList"><p>anything</p></div>
<div id="4" class="testList"><p>anything</p></div>
<div id="5" class="testList"><p>anything</p></div>
<div id="6" class="testList"><p>anything</p></div>
<div id="7" class="testList"><p>anything</p></div>
<div id="8" class="testList"><p>anything</p></div>
<div id="9" class="testList"><p>anything</p></div>

Example, I need to remove all div element's greater than and equal to id 5... My attempt is using a variable called clear

$(".testList:gt('"+clear+"') p").remove();

How do I use greater than or equal to in jquery?

like image 639
Yetimwork Beyene Avatar asked Jan 24 '26 20:01

Yetimwork Beyene


1 Answers

If you want to test against the ID, you need a .filtering function:

var clear = 5;

$('.testList').filter(function(i) {
    return (this.id >= clear);
}).remove();

http://jsfiddle.net/mblase75/jtYgJ/

WARNING: In HTML 4, IDs that start with a number are illegal, although most browsers will allow them anyway. HTML5 allows numerical IDs.

like image 80
Blazemonger Avatar answered Jan 26 '26 23:01

Blazemonger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!