![]() |
Printing multiple divs
Hello - fairly new to javascript - am hoping someone can help?
I am using this script: Code:
<script language="javascript">[HTML]<div id="div_print"> <img src="service/the-original-29.95-oil-change.jpg" width="900" height="483"> </div> <div class="print-button"> <span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#F00; font-weight:bold;">Please print this coupon and present to dealer at time of service</span><br /><br /> <input name="b_print" type="button" class="ipt" onClick="printdiv('div_print');" value=" Print "> </div><!--print-button-->[/HTML] This works exactly as I hoped. The problem arrises when I try to add a second instance, for example: [HTML]<div id="div_print"> <img src="service/the-original-29.95-oil-change.jpg" width="900" height="483"> </div> <div class="print-button"> <span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#F00; font-weight:bold;">Please print this coupon and present to dealer at time of service</span><br /><br /> <input name="b_print" type="button" class="ipt" onClick="printdiv('div_print');" value=" Print "> </div><!--print-button--> <br /><br /> <div id="div_print"> <img src="service/tire-rotation.jpg" width="900" height="483"> </div> <div class="print-button"> <span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#F00; font-weight:bold;">Please print this coupon and present to dealer at time of service</span><br /><br /> <input name="b_print" type="button" class="ipt" onClick="printdiv('div_print');" value=" Print "> </div><!--print-button-->[/HTML] No it does not work at all - both print buttons give the error: "Undefined". I was hoping to just use the same div container and just change the contents within that div. I appreciate any help /comments that can help me out. Thanks, Abe |
This line is a KILLER:
var newstr = document.all.item(printpage).innerHTML; document.all DOES NOT EXIST on any platforms except MSIE 8 and below. So on other platforms, that line will cause an error when the .item property is attempted. At which time the entire function fails. *KABLOOEY* ********* document.all has *NOT* been needed since the days of MSIE 4. 1997 or so. Are you really wanting to be compatible with a browser nobody has used in 10 years? |
It is far easier to set up classes for showing and hiding the parts of the page and simply attach the appropriate classes to the page to control which piece is to print rather than recreating the entire page.
language="javascript" was killed off about 10 years ago - at the moment toy should be using type="text/javascript" so as to support IE8 and modern browsers and once IE8 is dead the correct value of type="application/javascript" can be used. Your code copies the body of the page and then overwrites just the body with an entire generated page including a head tag that means the page you are trying to print has two <head> sections one after the other. Plus of course the antiquated document.all calls for IE4 instead of the document.getElementById calls that all modern browsers (and even IE5) use instead (but Old Pedant already told you about that bit). |
And then *THESE* are killers:
Code:
<div id="div_print">...</div>As soon as you added that second <div> with the same ID...*KABLOOEY*. |
This is a mildly hacky way to do what you want without mucking with a separate window.
Code:
<html> |
That would overwrite any classnames already on the elements.
To keep the existing classes and add a new on simply add a space and the new class name to the end of the existing class list. You should also use a regular expression to test for the class name with a boundary on either side in order to allow for it not being the only class in the class attribute. See http://javascriptexample.net/dom04.php for the sort of loop you'd have using getElementsByClassName that also adds that method for browsers that don't already have it. |
Old Pedant - that works just great :) I'll see if I can make it work (with graphical images etc) on Friday when I can next get to it. You're a life saver.
Thanks to others who also replied, but mostly it was telling me what I was doing wrong and not offering any way of making it work...thanks again Old Pedant :) |
Not quite out of the woods yet it seems....
The code below works great in IE, Firefox and Safari, however in Chrome it seems to only allow me to click on one of the buttons and then the other won't work. Using Chrome Version 23.0.1271.91 This is the code I am using (received from "Old Pedant" and modified to suit my particular needs). Maybe I screwed something up? Code:
<html> |
??? Just tried your code with *NO* changes in Chrome and it worked perfectly.
My version of Chrome: Version 23.0.1271.91 m -- so same as yours apparently. |
weird...I just tried again. The first try works..any subsequent trie nothing happens. maybe a re-install of chrome then :) Thanks again :)
|
Yep, re-installed and working fine now :)
|
Hmmm... doesn't instill confidence in Chrome's automatic upgrades, does it?
Glad it worked. |
| All times are GMT +1. The time now is 11:58 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.