davej Posted October 5, 2013 Share Posted October 5, 2013 Yes, a forward loop fails but a backwards loop works... <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Strip children from td's in table except links </title><script>window.onload = init;function init(){document.getElementById('btn1').onclick = tableloop;}function tableloop(){var table = document.getElementById('abc');var list = table.getElementsByTagName('td');for(var i=0 ; i<list.length ; i++){strip2(list[i]);}}function strip2(base){ //base parameter is a td elementvar list = base.childNodes; for(var i=list.length-1 ; i>=0 ; i--){if ( list[i] != null && list[i].nodeName != 'A' ){alert('deleting '+ i +':'+list[i].nodeName);base.removeChild(list[i]);}}}</script></head><body><table id="abc"><tr><td><a id="a1" href="">test</a><b>asdf</b>qwert qwert<b>werq</b><a href="">test</a>qwert</td><td><a id="a2" href="">test</a><b>asdf</b>qwert qwert<a href="">test</a><b>werq</b>qwert</td></tr><tr><td><a id="a3" href="">test</a><b>asdf</b>qwert qwert<b>werq</b>qwert<a href="">test</a></td><td><a id="a4" href="">test</a><a href="">test</a><b>asdf</b>qwert qwert<b>werq</b>qwert</td></tr></table><br/><input type="button" id="btn1" value="Strip Unwanted"/></body></html> Link to comment Share on other sites More sharing options...
astralaaron Posted October 5, 2013 Author Share Posted October 5, 2013 DaveJ your example worked fine. I was just doing multiple links to mess around and practice up a bit. I appreciate what you did. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now