javascript - How to get text of next td in jQuery? - Stack Overflow

I need Test 2 text when I clicked on "Click". I have tried like this ...$("#clicked"

I need Test 2 text when I clicked on "Click". I have tried like this ...

$("#clicked").click(function(){
  alert( $('this').closest('td').next('td').next('td').text());
})

But In alert there is empty. How I can get Test 2 ?

<tr>
  <td> </td>
  <td><a id = 'clicked'>Click</a></td>
  <td>Test 1</td>
  <td><a id = 'clicked2' /> Test 2</a></td>
</tr>

From ment below:

this means $('#clicked'). That means when I clicked on the Click link.

I need Test 2 text when I clicked on "Click". I have tried like this ...

$("#clicked").click(function(){
  alert( $('this').closest('td').next('td').next('td').text());
})

But In alert there is empty. How I can get Test 2 ?

<tr>
  <td> </td>
  <td><a id = 'clicked'>Click</a></td>
  <td>Test 1</td>
  <td><a id = 'clicked2' /> Test 2</a></td>
</tr>

From ment below:

this means $('#clicked'). That means when I clicked on the Click link.

Share Improve this question edited Aug 16, 2016 at 12:42 Mohibul Hasan Rana asked Aug 16, 2016 at 12:24 Mohibul Hasan RanaMohibul Hasan Rana 3472 gold badges4 silver badges18 bronze badges 9
  • What's $(this) for you? – ristapk Commented Aug 16, 2016 at 12:25
  • 1 $(this).parent().next().next().text(); – Riad Commented Aug 16, 2016 at 12:26
  • 1 Note that your second a element shouldn't have the / in the opening tag. – user1106925 Commented Aug 16, 2016 at 12:26
  • 1 @Riad: Why? That looks basically the same. – user1106925 Commented Aug 16, 2016 at 12:27
  • 1 Now your this is in quotes. Is your actual code like that? If so, that's your issue. If not, your code should work as is. Since you keep changing things, I have a feeling your actual code is different. Is that right? – user1106925 Commented Aug 16, 2016 at 12:55
 |  Show 4 more ments

4 Answers 4

Reset to default 3

you need to use parent instead of closest :

$("#clicked").click(function(){
  text = $(this).parent().next('td').next('td').text();
  alert(text);
})

https://jsfiddle/g6gnog4h/

$('#clicked').click(function(){
  console.log($(this).closest('td').next().text());
});

Try this one, as you need to go parent tr and than find the 4th td with the ID of a tag to get the text.

<table><tr>
  <td> </td>
  <td><a id = 'clicked'>Click</a></td>
  <td>Test 1</td>
  <td><a id = 'clicked2' /> Test 2</a></td>
</tr>
</table>

<script>
$('#clicked').click(function(){
    alert($(this).parents('tr').children().find('#clicked2').text());
});
</script>

You may try like this:

$('#clicked').click(function(){
  var val=$(this).siblings().text();
  alert(val);
});

siblings() method returns all sibling elements of the selected element.

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

相关推荐

  • javascript - How to get text of next td in jQuery? - Stack Overflow

    I need Test 2 text when I clicked on "Click". I have tried like this ...$("#clicked"

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信