vendredi 2 mai 2014

Is there a built-in method for selecting closest n siblings


Vote count:

2




Consider the structure below


Buttons


What i want is to select closest 9 buttons on clicking on a button and do lets say changing bg colors. And here's my custom code already doing that



$(document).on("click", "#footer button", function(){
var index = $(this).index();
var len = $("#footer button").length;
$("#footer button").css({
"background-color" : "#ccc"
});
if (index < 5) $("#footer button:lt(9)").css({
"background-color" : "#c99"
});
else if (index > (len - 6)) $("#footer button:gt(-10)").css({
"background-color" : "#c99"
});
else $("#footer button").slice((index -4), (index + 5)).css({
"background-color" : "#c99"
});
});


Now, i find using if .. else .. blocks for jquery selectors lame somehow. Of course we gotta use it if we have to but in this case do we? Are there any built-in methods to chain for such purpose in jquery?


HERE is the fiddle to play with.



asked 4 mins ago

Batu Zet

1,200





Aucun commentaire:

Enregistrer un commentaire