سؤال

How do you increment the value of a data attribute for all of the siblings of an object?

This works:

$('#id').data('position')+1

but this doesn't

$('#id').siblings().data('position') + 1;
هل كانت مفيدة؟

المحلول

Because .siblings() return an array of elements, therefore .data may not work. You have to iterate all element one by one like this

$('#id').siblings().each(function(){
   $(this).data('position') + 1; ///
})

نصائح أخرى

you need to iterate through siblings

var siblings=$('#id').siblings();
siblings.each(function(i,v){
   alert($(this).data('position') + 1);
   //do your stuff
})
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top