Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - Remove everything after an element

I have:

<div id="container">   <div id="i_1"></div>   <div id="i_2"></div>   <div id="i_3"></div>   <div id="i_4"></div> </div> 

How do I remove everything after $('#i_2')?

like image 912
Andy Hin Avatar asked Mar 06 '11 00:03

Andy Hin


People also ask

How to cut string after certain character?

Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .

How to destroy element in jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

How to remove all elements from div in jQuery?

The empty() method removes all child nodes and content from the selected elements. Note: This method does not remove the element itself, or its attributes. Tip: To remove the elements without removing data and events, use the detach() method. Tip: To remove the elements and its data and events, use the remove() method.

How to delete div from jQuery?

Use . remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .


2 Answers

Use nextAll

$('#i_2').nextAll().remove() 
like image 99
Adam Hopkinson Avatar answered Sep 23 '22 01:09

Adam Hopkinson


$("#container div:gt(1)").remove(); 

Another alternative.

like image 37
Joseadrian Avatar answered Sep 22 '22 01:09

Joseadrian