Thursday, 15 August 2013

JQuery if else statement stops at second condition

JQuery if else statement stops at second condition

I've been trying to fix this problem since last night and still no luck. I
badly need help in this. Here's the situation: I have 4 clickable images,
each with a clickCount textbox on my html file. The clickCount textbox
will display how many times the image was clicked. I got the first 2
images working, but when I click the 3rd image the clickCount textbox on
the 2nd image increments. Here's my code:
<script>
var targetId="";
$(".foods").click(function(event) {
if (event.target.id=="img1"){
targetId="img1clickCount";
addOrders();}
else if(event.target.id="img2"){
targetId="img2clickCount";
addOrders(); }
else if(event.target.id="img3"){
targetId="img3clickCount";
addOrders(); }
else if(event.target.id="img4"){
targetId="img4clickCount";
addOrders(); }
}); });
function addOrders(){
document.getElementById(targetId).value=parseInt(document.getElementById(targetId).
value)+1;
}
</script>
<body>
<table cellpadding=0 cellspacing=0 border=1 height=330
style="margin-left:18px">
<tr>
//Images
<td><img src="gangjeong.png" width="100" style="cursor:pointer" id="img1"
class="foods"></td>
<td><img src="daktoritang.png" width="100" style="cursor:pointer" id="img2"
class="foods"></td>
<td><img src="tangsuyuk.png" width="100" style="cursor:pointer" id="img3"
class="foods"></td>
<td><img src="tangsuyuk.png" width="100" style="cursor:pointer" id="img4"
class="foods"></td>
<tr height="5">
//Textboxes
<td><input type="text" maxlength="5" readonly="readonly" value="0"
id="img1clickCount">
</td>
<td><input type="text" maxlength="5" readonly="readonly" value="0"
id="img2clickCount">
</td>
<td><input type="text" maxlength="5" readonly="readonly" value="0"
id="img3clickCount">
</td>
<td><input type="text" maxlength="5" readonly="readonly" value="0"
id="img4clickCount">
</td>
</tr></table>

No comments:

Post a Comment