0

Have an issue with multidimensional javascript, here is an example of what Im trying to do There are td tags in tr tags like these

<tr id="tr">
 <td></td>
 <td></td>
 <td></td>
</tr>
<tr>
 <td></td>
 <td></td>
 <td></td>
</tr>

I want in every tr tag the last element to be deleted. This must happened on onchange event, but its not a point, I want the code to delete elements from the end in each tr tag. Here how I see it

var trs = document.getElementsTagByName("tr");
var tds = document.getElementsTagByName("td");
var tr = document.getElementById("tr");
var td = document.getElementById("td");
for(var q = 1, a = trs.length; q < a; a-1) {
    for(var w = 1, s = tds.length; w > s; s-1) {
        //Here I want to be a condition, if as an example there will be 2 td 
        //from each tr I want to be deleted in one time  from tr's elements
        while( !2 ) {
            td.parent.revomeChild("td");
        }
         break;
    }
}
2
  • Element id must be unique within the page Commented Apr 1, 2016 at 9:58
  • Fixed it still doesnt work, I need them to be deleted by 2 or 3 elements in a time, when condition change. Not sure how will i do it with last-child. Why downvote???? Commented Apr 1, 2016 at 10:02

1 Answer 1

2

Try this

var trs = document.getElementsByTagName('tr');
for(var i=0; i<trs.length; i++){
  trs[i].removeChild(trs[i].lastElementChild)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.