简体   繁体   中英

Get value from td after .closest() .find() .html()

I need to assign the value from a <td> to a variable. I find the <td> by using the jQuery closest() and find() methods.

If I alert the <td> it gives me the correct value but if I console.log() it it doesn't. It returns an object.

function getval(sel) {
  var thisTaskID = $(".selTaskOwner").on("change", function() {
    var currentRow = $(this).closest("tr");
    var foundTaskID = currentRow.find("td:eq(0)").html();
    alert(foundTaskID);
  });

  console.log(thisTaskID);
}

How do I get the value of the found <td> into a variable?

I've tried using text() , val() and html() at the end of thisTaskID but nothing works.

Thanks in advance, as you can guess I'm new

You get the value on selection change event, therefor you need no other function for it. Probably anything you need is this:

var foundTaskID = 0;
$(".selTaskOwner").on("change", function() {
    foundTaskID = $(this).closest("tr").find("td:eq(0)").html();
    /* Do what you want with it... */
    console.log(thisTaskID);
    alert(foundTaskID);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM