-1

I have a page where I have to print only div content, this content is dynamic and will vary based on drop downvalue. Now, i want to print this content, I was able to do it successfully using

function printDiv(divId){
    var orginalPage= document.body.innerHTML;
    var printDiv= document.getElementById(divId).innerHTML;
    document.body.innerHTML = printDiv;
    window.print();
    document.body.innerHTML = orginalPage;
}

but my problem is, when I am done printing the div and get back to actual page the dropdown value is getting reseting to default value. I want the value to be same as value which was selected before printing div.. for example I selected "xyz" from dropdown then after printing my div content I want xyz to be selected by default.

I will be iterating a list for this dropdown options. something like this

<select id="reason" >
<logic:iterate id="hs" name="hslist" type="hsData">
<option value="">select a reason </option>
<option value="<bean write name='hs' property='hsReasonId'/>">
<bean write name="hs" property="hsReasonDesc"/></option>
</logic>
</select>

Can someone help in fixing this...

3 Answers3

0

You can use the selected attribute for select element to set the default value

velen
  • 164
  • 1
  • 4
  • I want value to be, the value which user selected before printing the div...can I do that using selected attribute? – reddy reddy Sep 29 '16 at 02:12
0

You can solve with a small change.for that you want to open an extra window and assign the html in the window that you want to print.

function printDiv(divId){
var printDiv= document.getElementById(divId).innerHTML;
var myWindow = window.open("", "", "width=800,height=500");
myWindow.document.write(printDiv);
myWindow.print();
}
Pradyut Manna
  • 588
  • 1
  • 3
  • 12
-1

As you are assigning the innerHTML back to the body the value you selcted is earlier is lost. Try printing the innerHTML of body first. I think you will be able to figure out why your drop down is not populated back.