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); });
Comments
Post a Comment