javascript - What is the Best way to assign attributes by their index -


i have wrapper has children need specific positions.

let's have markup so:

<div class="wrapper">     <div class="slider" data-position=""></div>     <div class="slider" data-position=""></div>     <div class="slider" data-position=""></div> </div> 

the last child should have position of infront, second last child should have position of behind , every other slider should have position of limbo.

demo: http://jsfiddle.net/l5lpp/1/

i'm not fan of massive chain of if , else statements....

is there better more elegant way achieve this?

try this:

var sliders = $("div.slider").data("position", "limbo"); sliders.last().data("position", "infront"); sliders.eq(-2).data("position", "behind"); 

edit: using classes:

var sliders = $("div.slider").removeclass("limbo infront behind"); sliders.last().addclass("infront"); sliders.eq(-2).addclass("behind"); sliders.slice(0, -2).addclass("limbo"); 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -