/    Sign up×
Community /Pin to ProfileBookmark

Is there any way I can create a button that prints any given table on the page as apposed to the whole page? Any one know of any scripts or advice would be a great help.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@pyroSep 29.2003 — You could specify a [URL=http://www.alistapart.com/stories/goingtoprint/]print stylesheet[/URL]. Or, if you want to dynamically choose out which table to print, take a look at this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function printTable(obj) {
content = document.getElementById(obj).innerHTML;
newwin = window.open('');
newwin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"n',
'"http://www.w3.org/TR/html4/strict.dtd">n',
'<html>n',
'<head>n',
'<title>Printing...</title>n',
'<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">n',
'<body>n',
''+content+'n',
'</body>n',
'</html>');
newwin.print();
newwin.close();
}
</script>
</head>
<body>
<p>Print table: <a href="#" onclick="printTable('table0'); return false;">0</a> |
<a href="#" onclick="printTable('table1'); return false;">1</a> |
<a href="#" onclick="printTable('table2'); return false;">2</a></p>
<div id="table0">
<table>
<tr>
<td>Table Zero</td>
</tr>
</table>
</div>
<div id="table1">
<table>
<tr>
<td>Table One</td>
</tr>
</table>
</div>
<div id="table2">
<table>
<tr>
<td>Table Two</td>
</tr>
</table>
</div>
</body>
</html>
Copy linkTweet thisAlerts:
@jcongertonauthorSep 29.2003 — I tried the code you gave to me however it does not send anything to the printer? Or give me a print prompt? Sorry i'm very new to javascript.
×

Success!

Help @jcongerton 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 5.8,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...