Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display:none; displays 'none' in browser

This jsFiddle example works in Google Chrome, but in Internet Explorer then when the close icon is clicked the browser removes the pop-up element but results in the text 'none' being displayed in the browser window. Please explain how I can resolve this issue.

HTML:

<div id="popup">
    <!-- Close popup link -->
    <a href="javascript:document.getElementById('popup').style.display='none';">X</a>
</div>
like image 661
Web_Designer Avatar asked Apr 08 '11 16:04

Web_Designer


People also ask

What's the opposite of display none?

These elements have the initial value of inline . According to this answer in SO-EN display: none does not have an opposite as visibility:hidden (opposite visibility: visible ). The display property, however, decides what Layout rules an element will follow.

How do I set display to none in HTML?

display = "none"; To show an element, set the style display property to “block”.

Does display none work in Safari?

display = "none"; As you can see, it works in chrome, but it does not work in safari..


2 Answers

Use onclick for the event handler instead of href http://jsfiddle.net/AE2X3/4/

<div id="popup">
        <a href="#" onclick="document.getElementById('popup').style.display='none';return false;" id="close_popup"></a>
        <p>This is a pop-up.</p>
</div>
like image 152
amit_g Avatar answered Sep 19 '22 15:09

amit_g


I think what's happening is that the assignment is returning its result, and the browser is then displaying that. If you add void(0) to the end of your JavaScript, it won't be displayed.

Let me add that amit_g's answer is more correct than mine. He correctly points out that this sort of behaviour belongs in the OnClick handler, not in the href attribute.

like image 42
AgentConundrum Avatar answered Sep 19 '22 15:09

AgentConundrum