javascript - Catch element by class using index or eq in jquery -


html code:

<table border='1' cellpadding='5'>      <tr>         <td class="order"><a href="#">two</a></td>         <td>demo</td>         <td>last</td>     </tr>     <tr>         <td>two</td>         <td class="order"><a href="#">three two</a></td>         <td>sample</td>     </tr>     <tr>         <td>two</td>         <td class="order"><a href="#">five two</a></td>         <td>sample</td>     </tr>     <tr>         <td>five</td>         <td>quick</td>         <td class="order"><a href="#">nine</a></td>     </tr> </table> 

jquery code:

$('.order').click(function(){ var index = $(this).index();     var text = $(".order:eq(index-1)").text();     alert(text); }); 

on clicking order class want previous or next element same order class. iswrong code.

here fiddle

thank you.

index variable have add string in jquery like:

$(".order").click(function() {     var index = $(".order").index(this);     var text = $(".order:eq("+(index-1)+")").text();     alert(text); }); 

demo


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

Bubble Sort Manually a Linked List in Java -

asp.net mvc - SSO between MVCForum and Umbraco7 -