Monday 19 August 2013

jQuery can't stop infinite loop

jQuery can't stop infinite loop

I have the following functions:
jQuery.fn.extend({
itemOn: function () {
var id_selected = $(this).attr('data-id');
$(this).addClass('cart-product-sel');
$(this).find('.product-lightup').show();
$(this).find('.cart-plus').hide();
$(this).find('.cart-minus').show();
$.post(base_url()+'cart/addToCart/'+id_selected, function(row){
//Ritorna il rowid e lo imposta nell'item
$('li[data-id="'+id_selected+'"]').attr('data-rowid', row);
$.post(base_url()+'cart/showcart', function(output){
$('#cart-content').html(output);
});
});
$(this).attr('data-ison', '1');
console.log($(this).attr('data-ison'));
},
itemOff: function () {
var id_selected = $(this).attr('data-id');
var rowid = $(this).attr('data-rowid');
$(this).removeClass('cart-product-sel');
$(this).find('.product-lightup').hide();
$(this).find('.cart-plus').show();
$(this).find('.cart-minus').hide();
$.post(base_url()+'cart/removeFromCart/'+rowid, function(row){
$.post(base_url()+'cart/showcart', function(output){
$('#cart-content').html(output);
});
});
$(this).attr('data-ison', '0');
console.log($(this).attr('data-ison'));
}
});
I need to turn on (itemOn() function ) based on a table, I do the following:
[Table]
<tr class="cart-row">
<td>
<?= $cart_item['name'] ?>
</td>
<td>
<a href="#!" class="btn btn-primary btn-small qty-input"
data-type="number" data-name="item" data-pk="<?=
$cart_item['rowid'] ?>" data-id="<?= $cart_item['id'] ?>"
data-title="<?= $this->lang->line('cart_qty') ?>"> <?=
$cart_item['qty'] ?></a> <?=
$cart_item['options']['qty_description'] ?>
</td>
<td>
<?= $cart_item['price'] ?>/€ <?= $this->lang->line('cart_cad') ?>
</td>
</tr>
[jQuery]
$('.cart-row').each(function(event){
var ids = $(this).find('.qty-input').attr('data-id');
$('.cart-product[data-id="'+ids+'"]').itemOff();
})
I put a console.log() of the attribute ison to see what happens, and
actually it goes into an infinite loop.
I need to turn only those with data-id set and apply to a .cart-product
with the data-id equals to the data-id of the table. This works but
activates them in a loop.

No comments:

Post a Comment