Reputation: 299
How to kill the loop inside each? i mean here is my code when QUERY is has 100 results and somehow(i will be so happy if you would explain me why) the alert pops 100 times after clicking ONLY one one result (clicking on one result needs to pop the alert 1 time)
var tit_id = '#tit_' + this.SN;
function paintDATA(QUERY) {
var incld = '<div>';
$.each(QUERY, function () {
if (this.wCount != 0) {
$(tit_id).click(function () {
alert('RRRRRRRRRRRRRRR')
});
<div id="tit_' + shipment + '">
Upvotes: 2
Views: 135
Reputation: 83356
For each element returned by the query, you're attaching the same handler on the same element:
Did you want to attach the click handler on each item returned instead?
$.each(QUERY, function () {
if (this.wCount != 0) {
$(this).click(function () {
alert('RRRRRRRRRRRRRRR')
});
}
}
EDIT
So if you want to alert, then exit from the each loop, you can, like Jasper says, return false from the each callback:
$.each(QUERY, function () {
if (this.wCount != 0) {
$(this).click(function () {
alert('RRRRRRRRRRRRRRR')
return false;
});
}
}
Upvotes: 4
Reputation: 76003
I am not 100% sure what you're asking. If you want to break a $.each()
loop you can return false;
.
The documentation states:
We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
Documentation link: http://api.jquery.com/jquery.each/ (look just before the examples)
Upvotes: 2