473,769 Members | 5,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

my hide/display function fails when element begins display:none

function switch_display( switchme) {
var el = document.getEle mentById(switch me);
el.style.displa y = (el.style.displ ay == 'none')? '' : 'none';
}

im using this function to switch the display on and off of a given
element. BUT if the element is set to 'none' to begin with then it wont
display it.

please help...

Jan 15 '07 #1
5 2040
libsfan01 wrote on 16 jan 2007 in comp.lang.javas cript:
function switch_display( switchme) {
var el = document.getEle mentById(switch me);
el.style.displa y = (el.style.displ ay == 'none')? '' : 'none';
}

im using this function to switch the display on and off of a given
element. BUT if the element is set to 'none' to begin with then it wont
display it.
I doubt that, please show a working nonworking example.

What do you exactly mean by
"the element is set to 'none' to begin with"?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 15 '07 #2
ASM
libsfan01 a écrit :
function switch_display( switchme) {
var el = document.getEle mentById(switch me);
el.style.displa y = (el.style.displ ay == 'none')? '' : 'none';
}

im using this function to switch the display on and off of a given
element. BUT if the element is set to 'none' to begin with then it wont
display it.
Yes, of course.
Why ?
because if it isn't set to 'none' it's set to ''
that's to say : nothing comprehensive
or "normal" state ( witch is 'none' ! as set before)

following characteristic of element ( block or inline or ... )

el.style.displa y = (el.style.displ ay == 'none')? 'block' : 'none';
But always prefer :

function switcher(what) {
what = (typeof(what)== 'undefined')?
what.style : document.getEle mentById(what). style;
what.display = what.display==' '? 'none' : '';
}

And set your display element(s) to none via javascript.

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jan 16 '07 #3

Evertjan. wrote:
I doubt that, please show a working nonworking example.
Ok

When i put the display:none; command in the actual element e.g.
style='display: none;' it does work fine but when i put it in the css at
the top of the page (above the javascript, as a style applied to the
elements id) it wouldnt work? is there something wrong with the way i
was initially laying out the code on the page?

Jan 16 '07 #4
ASM
libsfan01 a écrit :
When i put the display:none; command in the actual element e.g.
style='display: none;' it does work fine but when i put it in the css at
the top of the page (above the javascript, as a style applied to the
elements id) it wouldnt work? is there something wrong with the way i
was initially laying out the code on the page?
JavaScript knows only JavaScript instructions
and
with style='display: none;' as attribute of element
JavaScript can find this attribute (style) using instruction :
element_style = document.getEle mentById('somet hing').style;

It is why it is better to give this attribute via JavaScript

Another way is to use css classes :

css :
=====
#truc { display: none; }
#truc.show { display: block; }

html :
======
<div id="truc"blah </div>
<p><a href="#" onclick="switch er('truc');retu rn false;">
hide/show
</a>
</p>

JS :
====
function switcher(whatId ) {
var s = document.getEle mentById(whatId );
s.className = s.className=='' ? 'show' : '';
}

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jan 16 '07 #5
libsfan01 wrote on 16 jan 2007 in comp.lang.javas cript:
>
Evertjan. wrote:
>I doubt that, please show a working nonworking example.

Ok
You say "ok" but I don't see that example.
When i put the display:none; command in the actual element e.g.
style='display: none;'
It is called an inline style setting, not a command.
it does work fine but when i put it in the css at
the top of the page (above the javascript, as a style applied to the
elements id)
it wouldnt work? is there something wrong with the way i
was initially laying out the code on the page?
How could we know if you keep the actual code secret?

Try:

=============== = test.html tested on IE7 ===============
<style type='text/css'>
#myId1 {display:none;}
.myClass {display:none;}
</style>

<div id='myId1'>This is it</div>
<button
onclick =
'alert(document .getElementById ("myId1").style .display);'>
click</button>

<div id='myId2' style='display: none;'>This is it too/two</div>
<button
onclick =
'alert(document .getElementById ("myId2").style .display);'>
click</button>

<div id='myId3' class='myClass' >This is it three</div>
<button
onclick =
'alert(document .getElementById ("myId3").style .display);'>
click</button>
=============== =============== =============== ===========

When you test and analyze the above,
now you know where the problem lies,
you can easily work aroud it, don't you think?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 16 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
40764
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div style="display:none"> can be displayed by setting the style attribute to "display:", or hidden with "display:none". This gives the illusion that the person filling out the form is switching from page to page...without the overhead of extra hits on the server,...
0
1590
by: Abhishek Sur | last post by:
dear all, i have a table wherein, upon clicking, some columns are hidden by the 'display:none' attribute set from javascript. as a result, the table is realigned. specifically, since the table is n x 2, whenever an element in column 0 is hidden, the corresponding element from column 1 is dragged across into column 1. this happens in IE6.0. the display:none can not be changed to visibility:hidden. is there some way to tell the browser...
1
5425
by: Jon W | last post by:
This is a small table with hover on the table cells. The first cell is setup to switch from div element to input element by use of display:block/none. In IE, onclick the input element is displayed correctly but it disappears if another hover is triggered. Any thoughts? Thanks,Jonny <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
1
2241
by: VK | last post by:
visibility:hidden vs display:none I guess I *feel* the difference (or maybe not) Could anyone put it in words for me?
2
9067
by: Jake Barnes | last post by:
Imagine I've this block of HTML: <p>Alex Schein Mailing List <input type="checkbox" name="newslettersToUse" value="133156"> (<a href="mcControlPanel.php" onClick="hideOrShowDivById('emailList133156'); return false;">See emails?</a>)</p> <form method="post" action="mcControlPanel.php" id="emailList133156" style="display:none;"><textarea name="formInputs">nazjeehaj@son.org,
8
4248
by: VK | last post by:
Hi, given a block element with some content and a link, is it possible to set this element display to none by CSS only? It has to be done only once. It is an absolutely positionned element so visibility:hidden will do as well. <div> <p>Some content</p> <p><a href="#">Hide</a></p>
4
70092
by: gimme_this_gimme_that | last post by:
Hi, I have an onchange method for a select box that goes something like this (the select is in a form named aForm): function page_on_change() { pageElement = aForm.my_page_id; aForm.nav_page_name.value = pages.value]; var si = pageElement.selectedIndex;
15
3165
by: cssExp | last post by:
hello, Rather than going on a wild explanation on what's the the problem, it'll be much quicker and easier if i let you look at it yourself, so I'll post my page source (actual contents taken out, of course). <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Language" content="en-us" />
2
19742
hsriat
by: hsriat | last post by:
All the elements which were set as style={display:none} are still being shown in Safari (Apple's Browser)... But when the same style is applied with javascript on an event, it hides the element. Like this... document.getElementById('abc').style.display='none'; This one is also not working: style="{background-image:url('/img/logo.png');background-repeat:no-repeat}" Both are working all good with Firefox, Opera and even IE 6 (which I...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.