I've tried to create a simple application to swap divs on clicking the respective buttons.
However, the first div which is the first-child of div.container should have 'left' and 'top' button disabled.
I've tried to achieve this by finding the first child and changing the property of the button inside it to disabled. But this is lost and is not retained on the new first child when I swap the first-child.
Here's the code.
<html>
<head>
<style>
.box {
height: 25%;
width: 45%;
padding: 1%;
margin-left: 1%;
margin-top: 1%;
border: 1px solid black;
float: left;
}
.disabled-btn {
cursor: not-allowed;
opacity: 0.25%;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div class="box" id="one">
<p>one</p>
<button class="right">Swap with right!</button>
<button class="left">Swap with left!</button>
<button class="top">Swap with top!</button>
<button class="down">Swap with down!</button>
</div>
<div class="box" id="two">
<p>two</p>
<button class="right">Swap with right!</button>
<button class="left">Swap with left!</button>
<button class="top">Swap with top!</button>
<button class="down">Swap with down!</button>
</div>
<div class="box" id="three">
<p>three</p>
<button class="right">Swap with right!</button>
<button class="left">Swap with left!</button>
<button class="top">Swap with top!</button>
<button class="down">Swap with down!</button>
</div>
<div class="box" id="four">
<p>four</p>
<button class="right">Swap with right!</button>
<button class="left">Swap with left!</button>
<button class="top">Swap with top!</button>
<button class="down">Swap with down!</button>
</div>
</div>
<script>
$(document).ready(function() {
resetEvents();
}
function resertEvents() {
$('.right').unbind('click');
$('.left').unbind('click');
$('.top').unbind('click');
$('.down').unbind('click');
$('.right').click(function(){
//alert('ok');
var toMove1 = $(this).parents('.box');
//toMove2 = toMove1.next();
$(toMove1).insertAfter($(toMove1).next());
});
$('.left').click(function(){
//alert('ok');
var toMove1 = $(this).parents('.box');
//toMove2 = toMove1.prev();
$(toMove1).insertBefore($(toMove1).prev());
});
$('.down').click(function(){
//alert('ok');
var toMove1 = $(this).parents('.box');
var middle = $(toMove1).next();
var toMove2 = $(middle).next();
toMove2 = $(toMove1).insertAfter($(middle).next());
var middle = $(toMove2).prev();
//alert(middle);
middle = $(middle).insertBefore($(middle).prev());
//toMove2 = toMove1.prev();
//$(toMove1).insertBefore($(toMove1).prev());
});
$('.top').click(function(){
//alert('ok');
var toMove1 = $(this).parents('.box');
var middle = $(toMove1).prev();
var toMove2 = $(middle).prev();
toMove2 = $(toMove1).insertBefore($(middle).prev());
var middle = $(toMove2).next();
//alert(middle);
middle = $(middle).insertAfter($(middle).next());
//toMove2 = toMove1.prev();
//$(toMove1).insertBefore($(toMove1).prev());
});
}
var firstChild = $('.container').children().first();
var lastChild = $('.container').children().last();
resetChild(firstChild, lastChild);
});
function resetChild(first, last) {
var firstChild = $('.container').children().first();
//console.log(firstChild);
if(firstChild){
//$(firstChild).find('.left').prop('disabled', true);
$(firstChild).find('.left').addClass(disabled-btn);
}
var lastChild = $('.container').children().last();
if(lastChild){
$(lastChild).find('.right').prop('disabled', true);
}
}
</script>
</body>
</html>
Added fiddle.