最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论