469,929 Members | 1,918 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

setting display to none

PJ6
When I call this function (I verified that I'm actually getting an element
with the id), why does IE ignore my attempt to set the display to none?

<script>
function test(id)
{
elem = document.getElementById(id)
elem.id.style = 'display: none;'
}
</script>

Paul
Sep 12 '05 #1
5 14755
PJ6 wrote:
When I call this function (I verified that I'm actually getting an element
with the id), why does IE ignore my attempt to set the display to none?

<script>
function test(id)
{
elem = document.getElementById(id)
elem.id.style = 'display: none;'
}
</script>

elem.style='display: none' //Not sure about this, though.

OR

function test(id){
document.getElementById(id).style.display = "none"
}
Mick
Sep 12 '05 #2
code is wrong

"PJ6" <no****@nowhere.net> wrote in message
news:RohVe.3887$3b6.186@trndny07...
When I call this function (I verified that I'm actually getting an element
with the id), why does IE ignore my attempt to set the display to none?

<script>
function test(id)
{
elem = document.getElementById(id)
elem.id.style = 'display: none;'
}
</script>

Paul

Sep 12 '05 #3
PJ6
Sorry, posted the wrong code...

The element's visibility is unchanged, although the alert tells me that its
property was properly set:

function test(id)
{
elem = document.getElementById(id)
if (elem.display == "block")
{
elem.display = "none"
}
else
{
elem.display = "block"
}
alert(elem.display)
}

Paul

"Mick White" <mw***********@rochester.rr.com> wrote in message
news:IM******************@twister.nyroc.rr.com...
PJ6 wrote:
When I call this function (I verified that I'm actually getting an
element with the id), why does IE ignore my attempt to set the display to
none?

<script>
function test(id)
{
elem = document.getElementById(id)
elem.id.style = 'display: none;'
}
</script>

elem.style='display: none' //Not sure about this, though.

OR

function test(id){
document.getElementById(id).style.display = "none"
}
Mick

Sep 12 '05 #4

PJ6 wrote:
Sorry, posted the wrong code...

The element's visibility is unchanged, although the alert tells me that its
property was properly set:

function test(id)
{
elem = document.getElementById(id)
if (elem.display == "block")
fix #1: if(elem.style.display == "block")
{
elem.display = "none"
fix #2: elem.style.display = "none";
}
else
{
elem.display = "block"
fix #3: elem.style.display = "block";
}
alert(elem.display)
}

Paul


Or if you would like to skip the if else statement altogether, you can
try something like this:

elem.style.display = (elem.style.display == "block") ? "none" :
"block";

Hope this helps. :)

Sep 12 '05 #5
PJ6 wrote:
Sorry, posted the wrong code...

The element's visibility is unchanged, although the alert tells me that its
property was properly set:

function test(id)
{
elem = document.getElementById(id)
if (elem.display == "block")
{
elem.display = "none"
}
else
{
elem.display = "block"
}
alert(elem.display)
}


The generic method is to toggle between 'none' and ''. That allows the
element to return to its default display (which may not be block and may
vary depending on the browser)

function showHide(id)
{
if (document.getElementById) {
var elem = document.getElementById(id)
if (elem.style) {
elem.style.display = ('' == elem.style.display)? 'none' : '';
}
}
}

[...]

--
Rob
Sep 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by relaxedrob | last post: by
7 posts views Thread by Christopher J. Hahn | last post: by
1 post views Thread by ca | last post: by
5 posts views Thread by verrice | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.