I am trying to remove all sibling elements after a specific div, say a div tag with id = id8.
<form>
<div id="id5">something ...<div>
<div id="id8">something ...<div>
<div id="id3">something ...<div>
<div id="id97">something ...<div>
<div id="id7">something ...<div>
...
<div id="idn">some text ...<div>
</form>
For this, I use the following code in jquery.
$("#id8 ~ div").remove();
It works fine in Firefox, but it doesn't work in IE7.
Is there an alternative way of archiving this using jquery and just specifying the tag id from the element that I want to start deleting elements? Thanks
Thank you all for your help. I get this solution based on the accepted answer.
function removeAfter(el,tag){
element = $('#'+el);
var aElements = $(tag,element.parent());
var index = (aElements.index(element));
for(i=(index+1);i<aElements.length;i++) {
$('#'+$(aElements.get(i)).attr('id')).remove();
}
}
just call
removeAfter('id8', 'div')
source
share