3

How do you change the css rule of #div1 in the style type="text/css" media="print" using javascript? I want to change the display to none on the click event of a checkbox/button.

<style type="text/css" media="screen">
 #div1{
 display:visible;
}
</style

<style type="text/css" media="print">
 #div1{
 display:visible;
}
</style

Thank You in advance

1
  • I do not want to hide the div from the screen,i only want to hide it while printing it,basically what im trying to do is give the user the option to print a section while printing a page Commented Feb 21, 2011 at 12:10

2 Answers 2

3

Create a new CSS class that is applied to whatever div you're trying to hide for a particular printing action, and add a rule to the print style sheet that hides it

<style type="text/css" media="print">
.print-hidden {
    display:none;
}
</style>

Your trigger might look something like:

<script>
document.getElementById('div1_trigger').onclick = function() {
    document.getElementById('div1').className = 'print-hidden';
}
</script>

Assuming you have multiple sections, you would also have to handle removing the print-hidden class when another section is triggered.

Also, display: visible; is not a valid CSS rule, you probably want display: block;

Sign up to request clarification or add additional context in comments.

Comments

0

I would probably use window.getComputesStyle method, it will give the actual style of the element:

window.getComputedStyle(div1,null).getPropertyValue("display")

See docs for details: https://developer.mozilla.org/en/DOM:window.getComputedStyle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.