Q:

Move one DIV element inside another using jQuery

0

Move one DIV element inside another using jQuery.

Move the following 
<div id="source">
...
</div>
To 
<div id="destination">
...
</div>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Move one DIV element inside another.</title>
</head>
<body>
<div id="nonSelected">
<!--Declaring BUTTON TAGS -->
<button id="btnDefault" onclick="moveButton(this)" type="button">Button1</button>
    <button id="btnPrimary" onclick="moveButton(this)" type="button" >Button2</button>
	
	  <button id="btnDanger" onclick="moveButton(this)" type="button">Button3</button>
	  
	  </div>
	  <div id="selected">
	  </div>
	  </body>
	  </html>

CSS Code :

#nonSelected{
    position: absolute;
    top: 10px;
    left: 10px;
    width: 180px;
    height: 180px;
    background-color: #884333;
    border-width: 1px;
    border-style: dotted;
    border-color: black;
    padding: 10px;
}

#selected{
    position: absolute;
    top: 10px;
    left: 200px;
    width: 180px;
    height: 180px;
    background-color: #498933;
    border-width: 1px;
    border-style: dotted;
    border-color: black;
    padding: 10px;
}

JavaScript Code :

function moveButton(elem){
    if( $(elem).parent().attr("id") == "nonSelected" ){
        $(elem).detach().appendTo('#selected');
    }
    else{
        $(elem).detach().appendTo('#nonSelected'); 
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now