The script below works fine when there is a single button on a page. However, when I add a second button to a page using a second CEWP, calling up a different webpart ID than the one below (WebpartWPQ3), both buttons call up the same webpart (the button that is last on the page appears to be the one that is called up by both buttons). The only difference in the script for the second button is the WebPartElementID (WebpartWPQ4). Is there something else I need to change?
<input type="button" OnClick="javascript:void(PrintWebPart())" value="Print Risks/Issues">
<script language="JavaScript" type="text/javascript">
//Controls which Web Part or zone to print
var WebPartElementID = “WebPartWPQ3”;
//Function to print Web Part
function PrintWebPart()
{
var bolWebPartFound = false;
if (document.getElementById != null)
{
//Create html to print in new window
var PrintingHTML = ‘<HTML>\n<HEAD>\n’;
//Take data from Head Tag
if (document.getElementsByTagName != null)
{
var HeadData= document.getElementsByTagName(“HEAD”);
if (HeadData.length > 0)
PrintingHTML += HeadData[0].innerHTML;
}
PrintingHTML += ‘\n</HEAD>\n<BODY>\n’;
var WebPartData = document.getElementById(WebPartElementID);
if (WebPartData != null)
{
PrintingHTML += WebPartData.innerHTML;
bolWebPartFound = true;
}
else
{
bolWebPartFound = false;
alert (‘Cannot Find Web Part’);
}
}
PrintingHTML += ‘\n</BODY>\n</HTML>’;
//Open new window to print
if (bolWebPartFound)
{
var PrintingWindow = window.open(“”,”PrintWebPart”, “toolbar,width=900,height=700,scrollbars,resizable,menubar”);
PrintingWindow.document.open();
PrintingWindow.document.write(PrintingHTML);
PrintingWindow.document.close();
// Open Print Window
PrintingWindow.print();
}
}</script>