user2917239
user2917239

Reputation: 898

jQuery modal append div, then prepend to previous div

I have a div named childDiv which lives inside parentDiv.

<div id="parentDiv">
    <p>Parent should be on top</p>
    <div id="childDiv">
        <p>Child</p>
    </div>
</div>

When the user clicks a button, I want childDiv to be appended to the modal dialog window. However, once the the user closes the modal, I want childDiv to go back to the parentDiv.

$(document).ready(function () {

    $("#myBtn").click(function () {
        $("#myModal").modal('show');
        $("#childDiv") .appendTo(".modal-body");
    });

    $('#myModal').on('hide.bs.modal', function (e) {
        $("#childDiv") .prependTo("parentDiv");        
        })
});

Please see the below fiddle. I would appreciate your help!

JSFiddle

Upvotes: 1

Views: 983

Answers (1)

Ram
Ram

Reputation: 144689

The selector is wrong, you are missing # for the ID selector:

$("#childDiv").prependTo("#parentDiv");

Upvotes: 1

Related Questions