javascript - How to get the <td> tag from table using jQuery? - Stack Overflow

I have a table with several rows and this is how one of the rows look like:<tr><td><div

I have a table with several rows and this is how one of the rows look like:

<tr>
    <td>
        <div class="entry_karma" id="entry_karma_9">
            <h4>4</h4>
        </div>

        <button type="button" class="btn btn-default btn-md" id="vote_up_9" name="vote_up_9" onclick="vote(9, 'up', 1, 8);" disabled="disabled">
            <span class="glyphicon glyphicon-arrow-up"></span>
        </button>

        <button type="button" class="btn btn-default btn-md" id="vote_down_9" name="vote_down_9" onclick="vote(9, 'down', 1, 6);">
            <span class="glyphicon glyphicon-arrow-down"></span>
        </button>

    </td>
    <td>Hello</td>
    <td>2014</td>
</tr>

I want to get the value of the div inside the first td in this tr. I have buttons in the same td and when I click them I go to a function and I need access to the row in that function.

I have tried the following

console.log($(this).closest("tr"));
console.log($(this).closest("tr").find(".div"));

but I keep getting

[prevObject: m.fn.init[1], context: undefined]

I want to access to this row and the access to the previous and next row as well but I am not able to see where I'm going wrong here.

Thank you.

I have a table with several rows and this is how one of the rows look like:

<tr>
    <td>
        <div class="entry_karma" id="entry_karma_9">
            <h4>4</h4>
        </div>

        <button type="button" class="btn btn-default btn-md" id="vote_up_9" name="vote_up_9" onclick="vote(9, 'up', 1, 8);" disabled="disabled">
            <span class="glyphicon glyphicon-arrow-up"></span>
        </button>

        <button type="button" class="btn btn-default btn-md" id="vote_down_9" name="vote_down_9" onclick="vote(9, 'down', 1, 6);">
            <span class="glyphicon glyphicon-arrow-down"></span>
        </button>

    </td>
    <td>Hello</td>
    <td>2014</td>
</tr>

I want to get the value of the div inside the first td in this tr. I have buttons in the same td and when I click them I go to a function and I need access to the row in that function.

I have tried the following

console.log($(this).closest("tr"));
console.log($(this).closest("tr").find(".div"));

but I keep getting

[prevObject: m.fn.init[1], context: undefined]

I want to access to this row and the access to the previous and next row as well but I am not able to see where I'm going wrong here.

Thank you.

Share Improve this question asked Jun 16, 2015 at 4:37 PetePete 7843 gold badges13 silver badges28 bronze badges 5
  • Replace this code console.log($(this).closest("tr").find(".div")); with console.log($(this).closest("tr").find(".entry_karma")); – stanze Commented Jun 16, 2015 at 4:40
  • When you say you want to get the value of the div, do you mean 4 or <h4>4</h4>? – Kilmazing Commented Jun 16, 2015 at 4:43
  • I want to get the value of the div inside the first td in this tr ::$(this).closest('tr').find('td:first div').html() – Shaunak D Commented Jun 16, 2015 at 4:44
  • I want to get the 4 or <h4>, either would do. – Pete Commented Jun 16, 2015 at 4:45
  • $(this).parents('tr') will get you the row. $(this).parents('td').find('div > h4').text() should get you 4 – Kilmazing Commented Jun 16, 2015 at 4:49
Add a ment  | 

6 Answers 6

Reset to default 1

You are looking for element with class="div", but there is nothing like that.

console.log($(this).closest("tr").find("div"));

Value (or better, content) of this div is in .html(), or .text().

console.log($(this).closest("tr").find("div").html());

You havn't defined any class name div. Try like this:

console.log($(this).closest("tr").find("div:first"));

Its not .div. Its just div. .div refers to an element with class div. so you have to refer either just div or div with class entry_karma

$(this).closest("tr").find("div.entry_karma");

Update

Try

$(this).closest("tr").find(".entry_karma>h4").text();

To get the value you need to do what @wahwahwah said and to get previous and next tr you can do it as below:

var prevTr=$(this).closest("tr").prev();
var nextTr=$(this).closest("tr").next();

Just replace .div with div, and find the closest td since the button is already inside the first td, like so:

console.log($(this).closest("td").find("div"));

P.S.: If you use .closest('tr'), it will find all existing divs (if you have more than one).

You can use a parent-child selector in jQuery that includes the element and the class:

var child = $('td > .entry_karma');

child now equals <h4>4</h4> where you can then retrieve the html and get 4

To get the parent <tr> element you can use the jQuery .has() selector using the same variable in the above var child = * snippet :

var child = $('td > .entry_karma');

console.log(child.html());

// get parent tr
var parent = $('tr').has(child);

console.log(parent.html());
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <div class="entry_karma" id="entry_karma_9">
        <h4>4</h4>
      </div>

      <button type="button" class="btn btn-default btn-md" id="vote_up_9" name="vote_up_9" onclick="vote(9, 'up', 1, 8);" disabled="disabled">
        <span class="glyphicon glyphicon-arrow-up"></span>
      </button>

      <button type="button" class="btn btn-default btn-md" id="vote_down_9" name="vote_down_9" onclick="vote(9, 'down', 1, 6);">
        <span class="glyphicon glyphicon-arrow-down"></span>
      </button>

    </td>
    <td>Hello</td>
    <td>2014</td>
  </tr>
</table>

Or, if the value is always going to be nested in the <h4> tag, you can access it directly like this:

var val = $('td > .entry_karma > h4').html();

var val = $('td > .entry_karma > h4').html();
console.log(val);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
      <tr>
        <td>
          <div class="entry_karma" id="entry_karma_9">
            <h4>4</h4>
          </div>

          <button type="button" class="btn btn-default btn-md" id="vote_up_9" name="vote_up_9" onclick="vote(9, 'up', 1, 8);" disabled="disabled">
            <span class="glyphicon glyphicon-arrow-up"></span>
          </button>

          <button type="button" class="btn btn-default btn-md" id="vote_down_9" name="vote_down_9" onclick="vote(9, 'down', 1, 6);">
            <span class="glyphicon glyphicon-arrow-down"></span>
          </button>

        </td>
        <td>Hello</td>
        <td>2014</td>
      </tr>
    </table>

In the button click handlers you can use the following for current, next, and previous rows, and the contents of the div:

var thisROW = $(this).closest('tr'),
    nextROW = thisROW.next(),
    prevROW = thisROW.prev(),
    div = $('div.entry_karma', thisROW).html();

$('button[id^="vote_down_"]').on('click', function() {
    var thisROW = $(this).closest('tr'),
        nextROW = thisROW.next(),
        prevROW = thisROW.prev(),
        div = $('div.entry_karma', thisROW).html();
    console.log( thisROW[0], nextROW[0], prevROW[0], div.trim() );
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <tbody>
<tr>
    <td>
        <div class="entry_karma" id="entry_karma_9">
            <h4>4</h4>
        </div>

        <button type="button" class="btn btn-default btn-md" id="vote_up_9" name="vote_up_9" onclick="vote(9, 'up', 1, 8);" disabled="disabled">
            <span class="glyphicon glyphicon-arrow-up"></span>
        </button>

        <button type="button" class="btn btn-default btn-md" id="vote_down_9" name="vote_down_9" onclick="vote(9, 'down', 1, 6);">
            <span class="glyphicon glyphicon-arrow-down"></span>
        </button>

    </td>
    <td>Hello</td>
    <td>2014</td>
</tr>
    </tbody>
  </table>

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信