vishrutsumit
asked on
javascipt function document.getElementById() gives error.
Hi,
here is a fucntion which I've written in javascript
function handlePrint()
{
document.write('<html><bod y>');
document.write('<table>');
var tableTemp=document.getElem entById("t able1");
alert(tableTemp.innerText) ;
//your selected table should have a id or name
document.write(tableTemp.i nnerHTML);
document.write('</table>') ;
document.write('</body></h tml>')
window.print();
}
in the above function, the line "var tableTemp=document.getElem entById("t able1"); " gives an error saying object required.
The table object with an id "table1" already exists.
Please help me out, Thanks in advance.
Vishrut
here is a fucntion which I've written in javascript
function handlePrint()
{
document.write('<html><bod
document.write('<table>');
var tableTemp=document.getElem
alert(tableTemp.innerText)
//your selected table should have a id or name
document.write(tableTemp.i
document.write('</table>')
document.write('</body></h
window.print();
}
in the above function, the line "var tableTemp=document.getElem
The table object with an id "table1" already exists.
Please help me out, Thanks in advance.
Vishrut
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ah, that's my bad, sorry *looks sheepish*
ASKER
Hi Zyloch/Bustarooms,
Great !!This works,Thanks a lot.only thing remained is that now I want to print the newly displayed page, and its not working,can any of you please suggest something.
Thanks in advance
Vishrut
Great !!This works,Thanks a lot.only thing remained is that now I want to print the newly displayed page, and its not working,can any of you please suggest something.
Thanks in advance
Vishrut
How come it's not working? Make sure your handlePrint function is in the <head> section.
ASKER
Actually the handlePrint method is getting called,but it doesnot print..I doono why
Well, you never know until you try ;) Try this:
document.write('<html><bod y onload="self.print();">');
document.write('<html><bod
ASKER
No It doesn't, this is my code :
function handlePrint()
{
alert("Print Called");
var tableTemp=document.getElem entById("t able1").in nerHTML;
document.write('<html><bod y onload="self.print();">');
document.write('<table>');
alert(tableTemp);
//your selected table should have a id or name
document.write(tableTemp);
document.write('</table>') ;
document.write('</body></h tml>');
window.print();
}
after my new window is displayed,and I go back by right clicking,then the print window appears,and it displays the old window.I want to display the new window which I just created.
function handlePrint()
{
alert("Print Called");
var tableTemp=document.getElem
document.write('<html><bod
document.write('<table>');
alert(tableTemp);
//your selected table should have a id or name
document.write(tableTemp);
document.write('</table>')
document.write('</body></h
window.print();
}
after my new window is displayed,and I go back by right clicking,then the print window appears,and it displays the old window.I want to display the new window which I just created.
How odd. This was my test:
<html>
<head>
<script>
function oh() {
document.write('hi');
}
</script>
</head>
<body>
<p onclick="oh();">fjdsoifdsh </p>
</body>
</html>
Look at Print Preview after you click the fjdsoifdsh.
<html>
<head>
<script>
function oh() {
document.write('hi');
}
</script>
</head>
<body>
<p onclick="oh();">fjdsoifdsh
</body>
</html>
Look at Print Preview after you click the fjdsoifdsh.
ASKER
I did that.
Acually in my case it worked,but only after I refrseh the window.Because when I refresh the onLoad even will be called,and the print is fired automatically.
I want that without refresh it should work.
Vishrut
Acually in my case it worked,but only after I refrseh the window.Because when I refresh the onLoad even will be called,and the print is fired automatically.
I want that without refresh it should work.
Vishrut
i guess your trying to make a printer friendly type of thing
if so there's a cleaner and nicer way using css and media types
if so there's a cleaner and nicer way using css and media types
ASKER
yes I'm trying to do the same.Please suggest a solution if you have so.
in my printer friendly page, I want to remove some of the headings and just want to show tabular data.
Please suggest.I'm also increasing the point value now.
in my printer friendly page, I want to remove some of the headings and just want to show tabular data.
Please suggest.I'm also increasing the point value now.
ok well this is ridiculously simplified, but it should give you the idea.
<html>
<head>
<style>
@media print {
.banners, .footer{display:none;}
}
</style>
</head>
<body>
<div class="banners">THIS IS A BANNER. WE DON'T WANT TO PRINT THIS</div>
<table id="table1">
<tr><td>Here's what I do want to print</td></tr>
</table>
<div class="footer">THIS IS SOME FOOTER INFORMATION. WE DON'T WANT TO PRINT THAT
EITHER.<BR/>
<a href="#" onclick="window.print();re turn false;">Print</a>
</div>
</body>
</html>
basically you can setup your page to hide things that you don't want printed (images, banners, navigation, etc) by using display:none; you can also do all kinds of fancy css stuff like adjust the text size of the stuff you do want printed. read up on it at http://www.yourhtmlsource.com/stylesheets/cssmediatypes.html
<html>
<head>
<style>
@media print {
.banners, .footer{display:none;}
}
</style>
</head>
<body>
<div class="banners">THIS IS A BANNER. WE DON'T WANT TO PRINT THIS</div>
<table id="table1">
<tr><td>Here's what I do want to print</td></tr>
</table>
<div class="footer">THIS IS SOME FOOTER INFORMATION. WE DON'T WANT TO PRINT THAT
EITHER.<BR/>
<a href="#" onclick="window.print();re
</div>
</body>
</html>
basically you can setup your page to hide things that you don't want printed (images, banners, navigation, etc) by using display:none; you can also do all kinds of fancy css stuff like adjust the text size of the stuff you do want printed. read up on it at http://www.yourhtmlsource.com/stylesheets/cssmediatypes.html
oh and this isnt limited to a javascript print call....it would print up like this if you go to File Print
ASKER
sorry guys..It is my first experience with this site,I couldn't divide the points correctly.
I regret for that.
anyway..will take care for future questions.
I regret for that.
anyway..will take care for future questions.
just curious if the css suggestion and link helped you figure it out
ASKER
Just looking at it,will appreciate if you please provide more help if you can.
thanks
vishrut
thanks
vishrut
To add on to what Bustarooms was saying, if you want something else to be invisible during viewing but visible during print, you'd just have this:
<style>
.visiblePrintOnly {display:none;}
@media print {
.banners, .footer{display:none;}
.visiblePrintOnly {display:block;}
}
</style>
<style>
.visiblePrintOnly {display:none;}
@media print {
.banners, .footer{display:none;}
.visiblePrintOnly {display:block;}
}
</style>
ASKER
ok,thanks a bunch.Let me work on this.
vishrut
vishrut
var tableTempText=document.get
document.write('<html><bod
document.write('<table>');
alert(tableTempText);
//your selected table should have a id or name
document.write(tableTempTe
document.write('</table>')
document.write('</body></h
window.print();