/    Sign up×
Community /Pin to ProfileBookmark

How can I copy a HTML table and remove all formatting?

I want to copy a HTML table from a webpage.
But I don’t want the width or anything else given for styling and size.
I’ve tried pasting into word (bad idea).
I’ve tried Excel. No luck.

In the end, I always have to go into the source code and manually mass replace font sizes, widths, heights etc – super annoying and time consuming!

Any other way?

Thanks

OM

to post a comment
HTML

6 Comments(s)

Copy linkTweet thisAlerts:
@cootheadAug 30.2015 — Hi there OM2,

[indent]

here is one possible solution...

[color=navy]
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>untitled document</title>

<style media="screen">
body {
background-color:#f0f0f0;
}
#conditioned {
display:block;
width:400px;
height:440px;
padding-left:10px;
border:1px solid #999;
margin:auto;
background-color:#fff;
font-family:'courier new',monospace;
}
</style>

</head>
<body>

<div id="mytable">
[color=green]
<TABLE BORDER="3" CELLSPACING="1" CELLPADDING="1">
<CAPTION>The Bradys</CAPTION>
<TR>
<TD ALIGN="center">Agnes</TD>
<TD ALIGN="center" style="width:300px;height:200px">Wilma</TD>
<TD ALIGN="center">George</TD>
</TR>
<TR>
<TD ALIGN="center">Gwen</TD>
<TD ALIGN="center" style="background-color:#f00">Skippy</TD>
<TD ALIGN="center">Alvin</TD>
</TR>
<TR>
<TD ALIGN="center">Harry</TD>
<TD ALIGN="center">Melvin</TD>
<TD ALIGN="center">Joe</TD>
</TR>
</TABLE>
[/color]
</div>

<textarea id="conditioned"></textarea>

<script>
(function() {
'use strict';

var t=document.getElementsByTagName('table'),
m=document.getElementById('mytable'),
j,k,l;

for(j=t[0].attributes.length-1;j>=0;j--){
t[0].removeAttributeNode(t[0].attributes[j]);
}

for(j=0;j<t[0].rows.length;j++){
for(k=0;k<t[0].rows[j].cells.length;k++){
for(l=t[0].rows[j].cells[k].attributes.length-1;l&gt;=0;l--){ <br/>
t[0].rows[j].cells[k].removeAttributeNode(t[0].rows[j].cells[k].attributes[l]);
}
}
}

m.innerHTML=m.innerHTML.replace(/&lt;/g,'&amp;lt;');
m.innerHTML=m.innerHTML.replace(/&gt;/g,'&amp;gt;');
document.getElementById('conditioned').innerHTML=m.innerHTML;
m.innerHTML='';
})();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;[/color]


[/indent]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@OM2authorAug 30.2015 — OMG! OMG! OMG!

@coothead, u are just so amazing. that is so awesome. u won't believe how much time i've wasted. my last solution was to print screen, crop to just the table, save as a file and upload to internet and then add to my html (trust me - this was quicker than mass editing HTML).

[B]question[/B]: how can i have a javascript input where i can dump code and then have transformed in the exiting output box you have?

+ i'd like to add a 1 pixel border to all cells

+ actually... it would be neat if i could have my own styling - like top row be bold and coloured background and first column same? Is this being too greedy? LOL

thanks!
Copy linkTweet thisAlerts:
@cootheadAug 30.2015 — Hi there OM2,

[indent]

something like this, perhaps...

[color=navy]
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;

&lt;meta charset="utf-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;

&lt;title&gt;untitled document&lt;/title&gt;

&lt;style media="screen"&gt;
body {
background-color:#f0f0f0;
}
#mytable {
margin:auto;
}
#mytable tr:first-of-type {
font-weight:bold;

}
#mytable tr:first-of-type td {
background-color:#eff;
}
#mytable td {
padding:5px 10px;
border:1px solid #999;
border-radius:5px;
background-color:#fff;
box-shadow:inset 0 0 5px #999;
}
#mytable td:first-of-type {
background-color:#fee;
}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

[color=green]
&lt;TABLE BORDER="3" CELLSPACING="1" CELLPADDING="1"&gt;
&lt;CAPTION&gt;The Bradys&lt;/CAPTION&gt;
&lt;TR&gt;
&lt;TD ALIGN="center"&gt;Agnes&lt;/TD&gt;
&lt;TD ALIGN="center" style="width:300px;height:200px"&gt;Wilma&lt;/TD&gt;
&lt;TD ALIGN="center"&gt;George&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD ALIGN="center"&gt;Gwen&lt;/TD&gt;
&lt;TD ALIGN="center" style="background-color:#f00"&gt;Skippy&lt;/TD&gt;
&lt;TD ALIGN="center"&gt;Alvin&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD ALIGN="center"&gt;Harry&lt;/TD&gt;
&lt;TD ALIGN="center"&gt;Melvin&lt;/TD&gt;
&lt;TD ALIGN="center"&gt;Joe&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TABLE&gt;
[/color]
&lt;script&gt;
(function() {
'use strict';

var t=document.getElementsByTagName('table'),
m=document.getElementById('mytable'),
j,k,l;

for(j=t[0].attributes.length-1;j&gt;=0;j--){
t[0].removeAttributeNode(t[0].attributes[j]);
}

for(j=0;j&lt;t[0].rows.length;j++){
for(k=0;k&lt;t[0].rows[j].cells.length;k++){
for(l=t[0].rows[j].cells[k].attributes.length-1;l&gt;=0;l--){ <br/>
t[0].rows[j].cells[k].removeAttributeNode(t[0].rows[j].cells[k].attributes[l]);
t[0].id='mytable';
}
}
}
})();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;[/color]


[/indent]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@OM2authorAug 31.2015 — wow, that code is soo neat ?

i think i'd be ok customising the css code u gave.

only thing i'd love to have is have an input and output code boxes + see what the output table looks like

not sure where to start
Copy linkTweet thisAlerts:
@cootheadSep 02.2015 — Hi there OM2,

[indent]

check out the attachment to see those options. ?

I have also prevented the removal of the "colspan" and "rowspan"

attributes to preserve the basic table structure.

[/indent]

[i]coothead[/i]

[canned-message]attachments-removed-during-migration[/canned-message]
Copy linkTweet thisAlerts:
@detazuSep 13.2015 — Just make and change code in css...

Thanks a lot of this explain.
×

Success!

Help @OM2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...