I am adding click handler into one of table elements. I confirmed it on inspect -> console that this is the returns the value that I need, the address value.
document.getElementById('donut-attributes').parentNode.childNodes[10].childNodes[1].childNodes[30].innerText //returns 123 Some Address on console log This is the complete script on main page:
<script> window.onload = function(){ var donutContainer = document.getElementById("donut-attributes"); donutContainer.addEventListener('click', function(e){ alert(e.target.parentNode); address = e.target.parentNode.childNodes[10].childNodes[1].childNodes[30].innerText; alert("donut container after"); }); } </script> I set up several alert() to make sure everything works. When it comes down to alert(e.target.parentNode), it shows [object HTMLTableRowElement]. However, when it comes down to alert(e.target.parentNode.childNodes[10]);, it returns undefined.
How can I fix the click handler so when I click any table element, I would get the address value stored into address? Why does it show the address on console log and it shows undefined when I used it with clickhandler?
EDIT: the table html (index.html.erb) looks something like this:
<table border=1 class="table table-condensed donut-attributes"> <tbody class="table-hover"> <tr> <td rowspan=5> Some_image </td> <tr> <td class="center" style="vertical-align: middle">Some_name</td> </tr> <tr> <td class="center" style="vertical-align: middle">Some_phone</td> </tr> <tr> <td class="center" style="vertical-align: middle">Some_rating</td> </tr> <tr> <td class="center" style="vertical-align: middle" id="address" >Some_address</td> </tr> <tr> <td rowspan=5> Some_image2 </td> <tr> <td class="center" style="vertical-align: middle">Some_name2</td> </tr> <tr> <td class="center" style="vertical-align: middle">Some_phone2</td> </tr> <tr> <td class="center" style="vertical-align: middle">Some_rating2</td> </tr> <tr> <td class="center" style="vertical-align: middle" id="address" >Some_address2</td> </tr> </tr> </tbody> How can I hover on any element on a table row, click it, and get the corresponding address? (i.e. if I hover and click on the second row, on any column in the second row, I need it to return some_address2)
childNodesincludes whitespace text nodes, and comment nodes between elements in counting. Use.childrenidvalues are not allowed in HTML.divtags cannot be children oftrtags..childNodes[10].childNodes[1].childNodes[30]seems really bad design... Can't you add a class or name to select? And you can not have a div as child in a table's row. That is not valid HTML.