javascript - Finding dom index based on selection -


html markup

<div>      <div class="selector"></div>   </div>  <div>     <div class="selector"></div>  </div>  <div>     <div class="selector1"></div>  </div>  <div>     <div class="selector1 active"></div>  </div> <div>     <div class="selector2"></div>  </div>  <div>     <div class="selector2"></div>  </div> 

based on user click, want find clicked bucket , based on bucket want find index.

javascript

var sectiontype = $(this).attr('class'); var sectionindex = sectiontype.find("active").index(); 

but not giving me selected/hover element index. can please me?

you can pass dom element index method, returns index of element in jquery collection.

$('div[class]').click(function () {     var cls = this.classname.split(' ')[0];     var index = $('div.' + cls).index(this); }); 

http://jsfiddle.net/kcpym/

index of .active element:

$('div[class]').click(function () {     var cls = this.classname.split(' ')[0],         $div = $('div.' + cls),         $active = $div.filter('.active'),         index = $div.index($active);      console.log(cls, index); }); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -