Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the last DIV using jQuery?

i have the following format :

<div id="container1">
<div id="post"> blah blah blah </div>
<div id="post"> blah blah blah </div>
<div id="post"> blah blah blah </div>
</div>

<div id="container2">
<div id="post"> blah blah blah </div>
<div id="post"> blah blah blah </div>
<div id="post"> blah blah blah </div>
</div>

I want a jQuery code to remove the last "post" DIV in the "container1" with a fading effect.

Important : the "container1" does not have a specified "post" DIVs number. so the code should just select the last "POST" div in the "container1" div.

Thanks

like image 224
CodeOverload Avatar asked Jan 29 '10 19:01

CodeOverload


2 Answers

$('#container1 #post:last').fadeOut() would remove the last div with the ID "post" in "container1".

Also, like Gumbo said, IDs should be unique. However, this jQuery code will still work.

like image 183
twernt Avatar answered Nov 15 '22 10:11

twernt


IDs must be unique in a document. So the selector #post will probably not work. But this should work anyway:

$("#container1").children("div[id=post]:last").fadeOut();
like image 32
Gumbo Avatar answered Nov 15 '22 09:11

Gumbo