Reputation: 1938
I have this fiddle. I want to use if else inside the filter function like this. The one I have works but the loop doesn't seem to break. Since it doesn't break, the else part is printed for all the not filtered conditions. How to break this loop? Or any better idea for this?
I have two textboxes and a button, when I enter caption and qty and hit add, I want to check if it already exists on the table. If it exists I want to replace the qty to new value. If it doesn't exists I want to add new row with new caption and qty. The checking of existence is mystery for me doesn't work at all.
$('#btnAdd').click(function () {
debugger;
UOMCaption = $('#UOMCaption').val();
UOMQty = $('#UOMQuantity').val();
$("#tbluom tr td:first-child").filter(function () {
if ($(this).text().indexOf(UOMCaption) > -1) {
$(this).next("td").text("found it");
}
else {
$('#tbluom >tbody').append('<tr><td>' + UOMCaption + '</td><td>' + UOMQty + '</td></tr>');
}
});
$('#UOMCaption,#UOMQuantity').val("");
});
Upvotes: 0
Views: 2273
Reputation: 388416
If you want the looping to break after first match then use
$("#tbluom tr td:first-child").each(function () {
if ($(this).text().indexOf(UOMCaption) > -1) {
$(this).next("td").text("found it");
return false;
} else {
$('#tbluom >tbody').append('<tr><td>' + UOMCaption + '</td><td>' + UOMQty + '</td></tr>');
}
});
Upvotes: 1