jquery remove all elements except for first one
Solution 1:
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();
Solution 2:
Or, as an alternative:
$('span').slice(1).remove();
slice()
Given a jQuery object that represents a set of DOM elements, the .slice() method constructs a new jQuery object containing a subset of the elements specified by the start and, optionally, end argument.start
Type: Integer
An integer indicating the 0-based position at which the elements begin to be selected. If negative, it indicates an offset from the end of the set.
Source: https://api.jquery.com/slice
Therefore, $('span').slice(1).remove()
will select, and remove, all elements after the first instance.
Solution 3:
Use this selector:
$('span:not(first-child)')
So your code is this:
$('span:not(first-child)').remove();