Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery remove all elements except for first one

Tags:

jquery

Using jquery remove how can i remove all the span tags except for the first one..

EDIT

 var html = var htm = $("#addin").find(".engagement_data:last-child").find(".keys_values").html();     html='        <span style="display:block;" class="k_v">          <innput type="text" class="e_keys" style="width:65px;" placeholder="key"/>          <input type="text" class="e_values" style="width:65px;" placeholder="value"/>        </span>        <span style="display:block;" class="k_v">          <input type="text" class="e_keys" style="width:65px;" placeholder="key"/>          <input type="text" class="e_values" style="width:65px;" placeholder="value"/>        </span> '; 
like image 333
Hulk Avatar asked Sep 18 '13 14:09

Hulk


People also ask

How do you remove all options from select in jQuery except first?

Explanation: If we want to remove all items from dropdown except the first item then we can use $('#ddlItems option:not(:first)'). remove(); Here we have excluded first item from being deleted. If we want to remove all items from dropdown except the last item then we can use $('#ddlItems option:not(:last)').

How to remove all elements 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 .

How to remove element inside div using jQuery?

Try: $('. chartsbar > div'). remove();

How to remove a class from an element in jQuery?

jQuery removeClass() Method The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.


1 Answers

Try with:

$(html).not(':first').remove(); 

or to be more specific:

$(html).not('span:first').remove(); 

To remove it from DOM, instead of html variable, use your selector:

$('#addin .engagement_data:last-child .keys_values').not('span:first').remove(); 
like image 144
hsz Avatar answered Sep 23 '22 19:09

hsz