javascript - Why isn't lastChild of a <tr/> element a <td/> element? -


i'm writing userscript reformat table, , i'm running don't understand. short version when ask lastchild of element, text, not last in row, i'd expect. using lastchild wrong?

the html looks this:

<tbody id="questions">   <tr class="drag-row" id="drag-row-4819" style="cursor: auto; left: 268.34375px;">     <td style="background-color: #eee; text-align: center; ;"><small class="muted">page 1</small></td>     <td style="width:15px;"></td>     <td class="text-right" style="width:35px;">1</td>     <td>do want fill out priority list?</td>     <td style="width:150px;">multiple choice (only 1 answer)</td>     <td>       <a href="/customers/4/accounts/2/portals/211/forums/forum_577/issues/issue_2797/survey_questions/4819">edit</a>       <a data-confirm="warning: sure want delete question?" data-method="delete" href="/customers/4/accounts/2/portals/211/forums/forum_577/issues/issue_2797/survey_questions/4819" rel="nofollow">delete</a>     </td>   </tr>   etc., more <tr/> elements here 

i'm trying separate "delete" link last element in each row new element @ end of each row.

my userscript code, works, this.

var rows = document.getelementbyid("questions").children,   separatedeletelink = function(row) {     var deletecell = row.children[row.children.length - 1];     if (deletecell.children.length == 2) {       var deletelink = deletecell.children[1];       console.log(deletelink);       row.appendchild(document.createelement("td")).appendchild(deletelink);     }   };  [].foreach.call(rows, separatedeletelink); 

for lines, example var deletecell = row.children[row.children.length - 1], i'd prefer write var deletecell = row.lastchild. when that, doesn't work. console.log(row.lastchild) returns object called #text.

by way, i'm new javascript, appreciate style or other tips on code. first, what's going on lastchild?

you looking row.lastelementchild.

notice dom composed nodes. nodes not elements, spaces, texts , other junk might not want work with.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -