I'm actually creating a shopping cart in JQuery where elements goes in a big box when you click on them. The problem is that I need "double click" to actually make the icons move to the box. By using prependTo or appendTo, the first click just move the icon to the left/right border. Why? Can you help me fix my code?
Let's see the bug in live...
Live demo: http://raphaelmartin.fr/kitcasino/jsfiddlebug.html
JSFiddle: http://jsfiddle.net/xbsoLp0u/2/
Code:
cart.html
<div id="selected"></div>
<div id="nonSelected">
<label for="blackjack"><img onclick="moveButton(this)" src="http://i.imgur.com/kfMWC91.jpg" alt="" data-checked='img/blackjack.gif' data-unchecked='img/blackjack.jpg'></label>
<INPUT id="blackjack" type="checkbox" value="Blackjack" name="game[]">
<label for="chuckaluck"><img onclick="moveButton(this)" src="http://i.imgur.com/AvPEx4i.jpg" alt="" data-checked='img/chuckaluck.gif' data-unchecked='img/chuckaluck.jpg'></label>
<INPUT id="chuckaluck" type="checkbox" value="Chuck a Luck" name="game[]">
<label for="roulette"><img onclick="moveButton(this)" src="http://i.imgur.com/tBEisp3.jpg" alt="" data-checked='img/roulette.gif' data-unchecked='img/roulette.jpg'></label>
<INPUT id="roulette" type="checkbox" value="Roulette" name="game[]">
<label for="stud"><img onclick="moveButton(this)" src="http://i.imgur.com/Oigq8QI.jpg" alt="" data-checked='img/stud.gif' data-unchecked='img/stud.jpg'></label>
<INPUT id="stud" type="checkbox" value="Stud Poker" name="game[]">
<label for="holdem"><img onclick="moveButton(this)" src="http://i.imgur.com/Oc52z68.jpg" alt="" data-checked='img/holdem.gif' data-unchecked='img/holdem.jpg'></label>
<INPUT id="holdem" type="checkbox" value="Holdem Poker" name="game[]">
<label for="boule"><img onclick="moveButton(this)" src="http://i.imgur.com/XFsfu7S.jpg" alt="" data-checked='img/boule.gif' data-unchecked='img/boule.jpg'></label>
<INPUT id="boule" type="checkbox" value="La Boule" name="game[]">
</div>
script.js
function moveButton(elem){
if( $(elem).parent().attr("id") == "nonSelected" ){
$(elem).detach().prependTo('#selected');
}
else{
$(elem).detach().prependTo('#nonSelected');
}
}