简体   繁体   中英

How to get value of a <td> onclick to an other <td> of a same row?

The fiddle is available here :

https://jsfiddle.net/syLuLob0/71/

As you can see , there is a listener of the 3rd column of the table. I would like to get the value of the 2nd column's td on click of the 3rd column cell.

For instance, if I click to "dead" the value will be "1047" and if I click to "ok" , the value will be "1048".

I did this function:

function showStatusDialog(){  
    for(var i = 0; i < 3; i++){
      alert(document.getElementById("TR"+i).childNodes[1].innerHTML);
     }
}

It's displaying all the values..

How can I achieve this ?

Thank you

first grab the parenNode then access the previous sibling by looking at the parents childNodes. then parse out an int from the sibling textContent property.

you can do it like so by changing your showStatusDialog function (the clickhandler):

function showStatusDialog(){  
    var sibling = this.parentNode.childNodes[1];
    var value = parseInt(sibling.textContent);
    console.log(value);
    //alert(document.getElementById("TR"+0).childNodes[1].innerHTML);

}

FIDDLE

you use jQuery selector to get the prev td value and parse the string

parseInt($(this).prev('td').text())

https://jsfiddle.net/syLuLob0/78/

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