Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I print only the selected <DIV>

Status
Not open for further replies.

tango1948

Programmer
Dec 23, 2006
111
IL
Hi
I have an HTML file where each (screen)page is a <DIV>.
I can select individual pages from a dropdown list or use
NEXT and PREV buttons. Each page has its DIV id which I know according to which page is currently selected.
I want to add a print button which will allow me to print only the current page, so I need some Javascript that will remove the div.printMe id from the page previousley printed and insert it for the current page, or some other method that will allow me to print only the current page.
Any tutorial links or code-snippets appriciated.

Thanks

David

 
Well, apply a known class to the DIVs that represent pages... something like:
Code:
<div class="pageDiv"><h1>Page 1</h1></div>
...
<div class="pageDiv"><h1>Page 5</h1></div>
...
<div class="pageDiv"><h1>Page N</h1></div>
When you click the print button, a script is run that gets all the DIV tags with class of "pageDiv" (using my example) and removes the class "printMe" from all those DIVs... then adds the class "printMe" to the DIV that represents the "current page" to be printed.

You would look at using getElementsByTagName('DIV') to get all the DIVs, then you would query the className property of each node returned (this shows a string representation of the class names attached to the node).

You can find out the "current page" to be printed by walking up the DOM from the position you click the button (assuming there is a "Print Me" button on each of the DIVs that represent a page to be printed).

Let us know how you go.

Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top