manuthalasseril
manuthalasseril

Reputation: 1064

get the siblingNode value in jquery

I want to get a siblings value here is my code

<table class="item-result-table" >
 <tr  class="class-tr">
  <td class="class-td"><label class="class-lbl">labeldata1</label></td>
  <td><label>labeldata2</label></td>
  <td>
    <input type="hidden" name="itmid[]"  value"itmid=something" />
    <input type="button" name="minus-name" class="minus" value="-" />
  </td>
 </tr>
</table>

I want to get the labeldata1 and I try these

$('.item-result-table').delegate('.minus', 'click', function(e) {
        alert(this.parentNode.parentNode.childNodes[0].childNodes[0].nodeValue);
});

results undefined

Any help is highly appreciated Thanks in advance

Upvotes: 0

Views: 94

Answers (3)

Ram
Ram

Reputation: 144729

Why not using jQuery:

$('.item-result-table').delegate('.minus', 'click', function(e) {
    alert($(this).closest('tr').find('label').first().text());
});

In case that you want to traverse the DOM using Vanilla JavaScript:

$('table').delegate('.minus', 'click', function (e) {
   console.log(this.parentNode.parentNode.children[0].children[0].textContent);
});

Upvotes: 1

Brett Zamir
Brett Zamir

Reputation: 14365

http://jsfiddle.net/GFT5a/3/

<table class="item-result-table">
    <tr  class="class-tr">
  <td class="class-td"><label class="class-lbl">labeldata1</label></td>
  <td><label>labeldata2</label></td>
  <td>
    <input type="hidden" name="itmid[]"  value"itmid=something" />
    <input type="button" name="minus-name" class="minus" value="-" />
  </td>
</tr>
</table>

<script>
    $('.item-result-table').delegate('.minus', 'click', function(e) {
        alert($(this).parent().siblings(':first-child').text());
    });
</script>

Upvotes: 1

user1908308
user1908308

Reputation:

First of all, I think you should use $(this) instead of this as it returns a jQuery-compatible object.

I'm fairly sure you want to use .siblings() which is described in the jQuery documentation.

Your posted HTML structure is difficult to test against, seeing as there is no .item-result-table class.

Upvotes: 0

Related Questions