php - How do I capture events from multiple dropdown-menus in Twitter Bootstrap? -


i have table contains multiple dropdown menus list of profile images. i've tagged list element db id of photo can perform associated action. i've coded table this:

<table class="table table-bordered"> <tbody> <tr>   <td><img src="/photos/files/5/m/131309a4fb918110ed1061e90a715eca.jpeg"/></td><td><div class="btn-group">   <a class="btn btn-primary" href="#"><i class="icon-user icon-white"></i> user</a>   <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>   <ul class="dropdown-menu">     <li id="5"><a href="#"><i class="icon-pencil"></i> edit</a></li>     <li id="5"><a href="#"><i class="icon-trash"></i> delete</a></li>   </ul> </div> </tr> <tr>   <td><img src="/photos/files/5/m/b19102d8ba1158e2a139ffa84e8e8540.jpeg"/></td><td><div class="btn-group">   <a class="btn btn-primary" href="#"><i class="icon-user icon-white"></i> user</a>   <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>   <ul class="dropdown-menu">     <li id="7"><a href="#"><i class="icon-pencil"></i> edit</a></li>     <li id="7"><a href="#"><i class="icon-trash"></i> delete</a></li>   </ul> </div></tr>     </tbody> </table> 

my jquery attempt below isn't working:

    $('.dropdown-menu').on('click', 'li', function(e){           if ($(this).attr('class')=='icon-pencil') {                 var id = $(this).attr('id');                 alert("id= " + id);          }      }); 

i never hit alert code missing here?

also there better way track photo id's or work way?

you have li > > .icon-pencil in structure you're looking this.classname on li click (which is, obviously, li). try this:

$('.dropdown-menu').on('click', 'li', function(){     if ($(this).find('icon-pencil').length > 0) {         var id = this.id;         alert("id= " + id);     } }); 

a cleaner approach listen click on anchor, since usual click element:

$('.dropdown-menu').on('click', 'a', function(){     if ($(this).children('.icon-pencil').length > 0) {         var id = $(this).closest('li').attr('id');         alert("id= " + id);     }     return false; }); 

and cleanest solution set id's on .dropdown-menu, unique, via .closest('ul') instead of .closest('li').

ps: having duplicate ids bad.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -