User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Deleting html tags using javascript Page [1]  
David0603
All American
12762 Posts
user info
edit post

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

BigMan157
no u
103352 Posts
user info
edit post

some combination of getelementbyid, getelementsbytagname, sethtml, and replace functions

6/25/2009 5:22:57 PM

David0603
All American
12762 Posts
user info
edit post

A tr close tag is not going to have an id.

6/25/2009 5:52:54 PM

BigMan157
no u
103352 Posts
user info
edit post

O RLY?

6/25/2009 6:15:01 PM

David0603
All American
12762 Posts
user info
edit post

Fuck this. I'm just going to use XPATH.

6/25/2009 6:30:43 PM

qntmfred
retired
40552 Posts
user info
edit post

you want to delete just the open and close tags but leave the contents?

Quote :
"A tr close tag is not going to have an id."


irrelevant.

6/25/2009 6:58:27 PM

David0603
All American
12762 Posts
user info
edit post

Yes.

6/25/2009 7:51:05 PM

BigMan157
no u
103352 Posts
user info
edit post


boobs = document.getElementsByTagName('table');
for(var titty in boobs) {
boobs[titty].setHTML(boobs[titty].innerHTML.replace(/</tr>\s*\S*<tr>/i,''));
}


that'll get rid of all of them, modify it to get rid of only the ones you want

6/25/2009 8:13:03 PM

David0603
All American
12762 Posts
user info
edit post

Error: invalid flag after regular expression
Source File: file:///C:/Documents%20and%20Settings/Administrator/Desktop/js.html
Line: 9, Column: 56
Source Code:
boobs[titty].setHTML(boobs[titty].innerHTML.replace(/</tr>\s*\S*<tr>/i,''));

6/25/2009 8:30:21 PM

BigMan157
no u
103352 Posts
user info
edit post

whoops

/</tr>\s*\S*<tr>/i should be /<\/tr>\s*\S*<tr>/i

6/25/2009 8:34:31 PM

David0603
All American
12762 Posts
user info
edit post

Error: boobs[titty].setHTML is not a function
Source File: file:///C:/Documents%20and%20Settings/Administrator/Desktop/js.html
Line: 9

6/25/2009 8:39:26 PM

BigMan157
no u
103352 Posts
user info
edit post

goddamn jquery has made it impossible for me to do normal js

boobs[titty].innerHTML = boobs[titty].innerHTML.replace(/</tr>\s*\S*<tr>/i,'');


p.s. lol Error boobs[titty]

UPDATE: fuck it, new plan


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);
}
}
}


[Edited on June 25, 2009 at 8:54 PM. Reason : that should maybe kinda work]

6/25/2009 8:41:22 PM

David0603
All American
12762 Posts
user info
edit post

boobs[titty].innerHTML = boobs[titty].innerHTML.replace(/<\/tr>\s*\S*<tr>/i,'');

Fantastic.

Thanks man.

6/25/2009 8:45:24 PM

qntmfred
retired
40552 Posts
user info
edit post

+1 points for use of regex

imo 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

David0603
All American
12762 Posts
user info
edit post

Yeah, I really need a refresher on regex.

6/25/2009 10:25:57 PM

David0603
All American
12762 Posts
user info
edit post

Error: boobs[titty][nipple].parent has no properties
Source File: file:///C:/Documents%20and%20Settings/Administrator/Desktop/js.html
Line: 14

6/25/2009 10:28:06 PM

philihp
All American
8349 Posts
user info
edit post

I wish I would come across code like this more often.

6/25/2009 11:52:43 PM

BigMan157
no u
103352 Posts
user info
edit post

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]);
}
}


you can also maybe use boobs[titty] in place of breasts[nipple].parent, but if there's a tbody tag there or the browser creates one automatically, that'd fuck things up

also, i'm running out of boob words

also also if you have an odd number of cells you're probably gonna lose one, but you should be able to figure it out from there by setting a counter and comparing it to breasts.length

[Edited on June 26, 2009 at 5:02 PM. Reason : i which i could use those variable names professionally]

6/26/2009 5:00:14 PM

David0603
All American
12762 Posts
user info
edit post

Error: breasts[nipple].parent has no properties
Source File: file:///C:/Documents%20and%20Settings/Administrator/Desktop/js.html
Line: 22

6/26/2009 5:53:10 PM

LimpyNuts
All American
16859 Posts
user info
edit post

^^ melons, jugs, sweater pillows (aka sweater cows)

6/26/2009 6:30:46 PM

BigMan157
no u
103352 Posts
user info
edit post

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);
}
}


i forgot the for...in statement gets ALL objects, and the inner for...in wasn't playing nice

[Edited on June 26, 2009 at 6:36 PM. Reason : and i coulda sworn it was parent not parentNode, but i guess i had it reversed?]

6/26/2009 6:35:29 PM

David0603
All American
12762 Posts
user info
edit post

sweeeeeeeeeeeeeeet

6/26/2009 8:27:22 PM

Optimum
All American
13716 Posts
user info
edit post

best. thread. ever.

6/26/2009 8:35:19 PM

David0603
All American
12762 Posts
user info
edit post

Just curious what is the purpose of /^[0-9]+$/ and tbody?

6/26/2009 8:35:29 PM

BigMan157
no u
103352 Posts
user info
edit post

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

FroshKiller
All American
51908 Posts
user info
edit post

Not to split hairs, but tables shouldn't "technically have tbodies" according to spec.

6/27/2009 9:59:35 AM

BigMan157
no u
103352 Posts
user info
edit post

not to split hairs, but that is splitting hairs since there's only one condition where it isn't required

and 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

FroshKiller
All American
51908 Posts
user info
edit post

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

ScHpEnXeL
Suspended
32613 Posts
user info
edit post

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

BigMan157
no u
103352 Posts
user info
edit post

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

FroshKiller
All American
51908 Posts
user info
edit post

In other words, it's not required, as none of those other elements are required, either.

6/27/2009 11:39:52 AM

BigMan157
no u
103352 Posts
user info
edit post

Quote :
"and the fact that that condition is how tables are used 95%+ of the time is neither here nor there sir!"

6/27/2009 11:41:27 AM

 Message Boards » Tech Talk » Deleting html tags using javascript Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.