javascript - jquery - get the position number of clicked td in a table - Stack Overflow

<table><tr><td>me<td><td>you<td><td>we<td><tr>

<table>
   <tr>
      <td>me</td>
      <td>you</td>
      <td>we</td>
   </tr>
</table>

Using Jquery i want to know which childrin was clicked and get the number of this childrin. like, if I click you, i want to get 2 and if we, then 3.. if me, then 1.

is there something like .nthchild(); in Jquery?

here is my fiddle to test: /

<table>
   <tr>
      <td>me</td>
      <td>you</td>
      <td>we</td>
   </tr>
</table>

Using Jquery i want to know which childrin was clicked and get the number of this childrin. like, if I click you, i want to get 2 and if we, then 3.. if me, then 1.

is there something like .nthchild(); in Jquery?

here is my fiddle to test: http://jsfiddle/vfp9x/

Share Improve this question edited Jul 1, 2014 at 7:01 doniyor asked Jul 1, 2014 at 6:53 doniyordoniyor 38k61 gold badges181 silver badges270 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Simply use $(this).index(), you also have wrong id for table in live demo, it should be table1

Remember the index is zero based so you will get zero 0 for first element and 1 for second element and so on.

Live Demo

$('#table1').on('click', 'td', function () {    
    $('#out').text($(this).index()+1);
});

.index()

If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

Try,

$('#table1').on('click','td',function(){
    var child_number = $(this).closest('tr').find('td').index(this);
    $('#out').text(child_number);
});

DEMO


Simply use .index() as per the other answer provider suggested, and also keep this way of getting the index also. it would helpful for you in some other contexts, like getting the index of the current element from a collection of elements (not siblings to each other)

Try Like

$("#ul").on('click', 'td', function () {
    debugger;
    alert(this.id);    
});

Demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745543391a4632230.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信