By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

setting display to none

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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.