mercredi 25 février 2015

Unselect checkbox at click using Jquery

I set up a basic shopping cart where you have to put goods into a green box to order them. When you click on the item icon, it correctly checks the associated checkbox. Unfortunately, when you click again on it, it doesn't uncheck.


I've tried `$(elem).prop('checked', false); but it doesn't work. Do you have any other idea please?


JSFiddle: http://ift.tt/1vy7YDh (I've let the checkbox displayed on to let you see the bug).


Code:


html



<div id="selected"></div>

<div id="nonSelected">

<label for="blackjack"><img onclick="moveButton(this)" src="http://ift.tt/1CYpQoi" 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://ift.tt/19WTbbK" 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://ift.tt/1CYpQok" 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://ift.tt/19WT9k7" 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://ift.tt/1CYpRIW" 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://ift.tt/19WTbbU" alt="" data-checked='img/boule.gif' data-unchecked='img/boule.jpg'></label>
<INPUT id="boule" type="checkbox" value="La Boule" name="game[]">

</div>


js



function moveButton(elem){
if( $(elem).closest("div").attr("id") == "nonSelected" ){
$(elem).prependTo('#selected');
}
else{
$(elem).prependTo('#nonSelected');
}
}


css



#selected{
width: 400px;
height: 200px;
background-color: #339933;
border-width: 1px;
border-style: dotted;
border-color: black;
padding: 10px;
margin: 10px;
}

img{
cursor: pointer;
}




Aucun commentaire:

Enregistrer un commentaire