Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding DIV with a close button, dynamically to another DIV using Jquery

Tags:

jquery

I would like to add 4 divs with close buttons on top right corner , aligned horizontally to a parent DIV. When the close button is clicked, the div should be removed from the parent DIV. Is this possible with Jquery ?if someone could post a sample code i would really appreciate it !

Thanks

like image 333
user636525 Avatar asked Nov 30 '25 09:11

user636525


1 Answers

Let's help you.

jsBin demo

<div class="box">
    <div class="close_box">X</div>

    <h2>Box title</h2>
    <p>Merol muspi rolod tis tema...</p>

</div>

jQ:

$(document).on('click','.close_box',function(){
    $(this).parent().fadeTo(300,0,function(){
          $(this).remove();
    });
});

or simply:

$(document).on('click','.close_box',function(){
    $(this).parent().remove();
});

Take a look at the docs (always!):

http://api.jquery.com/on/
http://api.jquery.com/parent/
http://api.jquery.com/remove

like image 135
Roko C. Buljan Avatar answered Dec 01 '25 22:12

Roko C. Buljan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!