I'm trying to modify an html template which I do not have access to until runtime.How can I use javascript to delete every odd </tr> and <tr> tag?Basically I have a twelve item one column list and I want to turn it into a two column list.
6/25/2009 5:16:00 PM
some combination of getelementbyid, getelementsbytagname, sethtml, and replace functions
6/25/2009 5:22:57 PM
A tr close tag is not going to have an id.
6/25/2009 5:52:54 PM
O RLY?
6/25/2009 6:15:01 PM
Fuck this. I'm just going to use XPATH.
6/25/2009 6:30:43 PM
you want to delete just the open and close tags but leave the contents?
6/25/2009 6:58:27 PM
Yes.
6/25/2009 7:51:05 PM
boobs = document.getElementsByTagName('table');for(var titty in boobs) { boobs[titty].setHTML(boobs[titty].innerHTML.replace(/</tr>\s*\S*<tr>/i,''));}
6/25/2009 8:13:03 PM
Error: invalid flag after regular expressionSource File: file:///C:/Documents%20and%20Settings/Administrator/Desktop/js.htmlLine: 9, Column: 56Source Code: boobs[titty].setHTML(boobs[titty].innerHTML.replace(/</tr>\s*\S*<tr>/i,''));
6/25/2009 8:30:21 PM
whoops/</tr>\s*\S*<tr>/i should be /<\/tr>\s*\S*<tr>/i
6/25/2009 8:34:31 PM
Error: boobs[titty].setHTML is not a functionSource File: file:///C:/Documents%20and%20Settings/Administrator/Desktop/js.htmlLine: 9
6/25/2009 8:39:26 PM
goddamn jquery has made it impossible for me to do normal js
boobs[titty].innerHTML = boobs[titty].innerHTML.replace(/</tr>\s*\S*<tr>/i,'');
boobs = document.getElementsByTagName('table');for(var titty in boobs) { boobs[titty].getElementsByTagName('tr'); titCount = 0; boobStr = ""; for(var nipple in boobs[titty]) { boobStr += boobs[titty][nipple].innerHTML; boobs[titty][nipple].parent.removeChild(boobs[titty][nipple]); titCount++; if(!(titCount%2)) { breast = document.createElement('tr'); breast.innerHTML = boobStr; boobStr = ''; boobs[titty][nipple].parent.appendChild(breast); } }}
6/25/2009 8:41:22 PM
boobs[titty].innerHTML = boobs[titty].innerHTML.replace(/<\/tr>\s*\S*<tr>/i,'');Fantastic.Thanks man.
6/25/2009 8:45:24 PM
+1 points for use of regeximo it would have been simpler (easier to read/manage) to do a variation of getElementsByTagName('tr');[Edited on June 25, 2009 at 9:31 PM. Reason : nm i just saw bigman's fuck it new plan strategy. go with that ]
6/25/2009 9:31:08 PM
Yeah, I really need a refresher on regex.
6/25/2009 10:25:57 PM
Error: boobs[titty][nipple].parent has no propertiesSource File: file:///C:/Documents%20and%20Settings/Administrator/Desktop/js.htmlLine: 14
6/25/2009 10:28:06 PM
I wish I would come across code like this more often.
6/25/2009 11:52:43 PM
whoops
boobs = document.getElementsByTagName('table');for(var titty in boobs) { breasts = boobs[titty].getElementsByTagName('tr'); titCount = 0; boobStr = ""; for(var nipple in breasts) { boobStr += breasts[nipple].innerHTML; titCount++; if(!(titCount%2)) { breast = document.createElement('tr'); breast.innerHTML = boobStr; boobStr = ''; breasts[nipple].parent.appendChild(breast); } breasts[nipple].parent.removeChild(breasts[nipple]); }}
6/26/2009 5:00:14 PM
Error: breasts[nipple].parent has no propertiesSource File: file:///C:/Documents%20and%20Settings/Administrator/Desktop/js.htmlLine: 22
6/26/2009 5:53:10 PM
^^ melons, jugs, sweater pillows (aka sweater cows)
6/26/2009 6:30:46 PM
try out this
boobs = document.getElementsByTagName('table');for(var titty in boobs) { saggies = boobs[titty]; if(titty.match(/^[0-9]+$/)) { tempBra = document.createElement('table'); tempRack = document.createElement('tbody'); tempBra.appendChild(tempRack); breasts = boobs[titty].getElementsByTagName('tr'); titCount = 0; boobStr = ""; for(var nipple=0; nipple<breasts.length; nipple++) { boobStr += breasts[nipple].innerHTML; titCount++; if(!(titCount%2)) { breast = document.createElement('tr'); breast.innerHTML = boobStr; boobStr = ""; tempRack.appendChild(breast); } } saggies.parentNode.insertBefore(tempBra, saggies); saggies.parentNode.removeChild(saggies); }}
6/26/2009 6:35:29 PM
sweeeeeeeeeeeeeeet
6/26/2009 8:27:22 PM
best. thread. ever.
6/26/2009 8:35:19 PM
Just curious what is the purpose of /^[0-9]+$/ and tbody?
6/26/2009 8:35:29 PM
tables should technically have tbodies, but most people generally don't put them in there - some browsers get pissy should you leave it out when creating/manipulating a table DOM element though js and just wont work/^[0-9]+$/ just makes sure the index is a number (^means the beginning of the string, $ means the end of the string, [0-9]+ means match only characters 0-9 at least once)the reason it's in the if statement in the for...in is because the for...in will return everything associated with the reference object. if you have 4 tables it'll return 0 through 3 and then .length and .otherstuff - when you evaluate the numerics you'll get a reference to a DOM element, when you evaluate .length you don't and the script would break if you don't screen for it
6/27/2009 9:39:39 AM
Not to split hairs, but tables shouldn't "technically have tbodies" according to spec.
6/27/2009 9:59:35 AM
not to split hairs, but that is splitting hairs since there's only one condition where it isn't requiredand the fact that that condition is how tables are used 95%+ of the time is neither here nor there sir!
6/27/2009 11:16:06 AM
It's not required. TABLE, open TR, open TD, close TABLE is perfectly legit and will validate just fine. TBODY is an optional element for describing the semantic structure of a table.
6/27/2009 11:28:59 AM
ahhaha. i have no idea what most of these things mean.. but the use of boobs/titties is awesome
6/27/2009 11:34:06 AM
tbody is only not required when there is no thead, tfoot, and no other tbodies in a table
6/27/2009 11:38:07 AM
In other words, it's not required, as none of those other elements are required, either.
6/27/2009 11:39:52 AM
6/27/2009 11:41:27 AM