Reply
problem editing CSS display attribute
Old 06-21-2008, 05:04 PM problem editing CSS display attribute
pealo86's Avatar
Webmaster Talker

Posts: 651
Name: Matt
Location: England, north west
Trades: 0
I have the following code:

CSS:
Code:
    
.box, .boxHide {
    border:#CCC solid 1px;
    width:300px;}
    
.boxHide {
    display:none;}
Javascript
Code:
function appear (element)
    {
        var element = document.getElementById(element);
        var elementStyle = element.style;
        
        if (elementStyle.display == 'none')
            {
                // make the element visible
                elementStyle.display = "block";
            }
        
        else
            {
                //make the element invisible
                elementStyle.display = "none";
            }
    }
But I seem to have to click twice in order for it to work properly. Ive figured out [through using the alert function to show the elements 'display:' property from the CSS] that nothing seems to be assigned to it. The alertbox just shows up blank.

Then, since its not equal to 'none'; is assigned 'none' to its display. Therefore it starts working after 2 clicks.

But I just cant figure out why element.style.display isnt equal to 'none' when Ive set it in the CSS??
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
When You Register, These Ads Go Away!
Old 06-21-2008, 05:13 PM Re: problem editing CSS display attribute
Novice Talker

Posts: 5
Trades: 0
Had this problem myself before, to fix it I removed the display from css and added it into the style tag and it worked.
Asiri is offline
Reply With Quote
View Public Profile
 
Old 06-21-2008, 05:16 PM Re: problem editing CSS display attribute
wayfarer07's Avatar
I like pie

Posts: 3,367
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
this line:
Code:
if (elementStyle.display == 'none')
is looking for the inline attribute style="display: none;". It doesn't know about the style in your external stylesheet when you check it this way, which is why you must click it twice for it to see that it is there.

***Edit*** oops, you beat me to it. Good job.
__________________
Wayfarer | jQuery Tooltip | Mapbox: the jQuery Map
Latest Project: Houston Movers
If Google is the Coca-Cola of Web search, Bing is RC Cola

Last edited by wayfarer07; 06-21-2008 at 05:17 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 06-21-2008, 05:19 PM Re: problem editing CSS display attribute
pealo86's Avatar
Webmaster Talker

Posts: 651
Name: Matt
Location: England, north west
Trades: 0
ahhhh so it won't work with an external stylesheet? Thats horrible!
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 06-21-2008, 05:30 PM Re: problem editing CSS display attribute
wayfarer07's Avatar
I like pie

Posts: 3,367
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
The way I deal with it, to avoid writing inline styles to start out, is to load them dynamically on page load from within the script, or else (normally) to place the needed styles within classes, and simply switch the styles with the className method.
__________________
Wayfarer | jQuery Tooltip | Mapbox: the jQuery Map
Latest Project: Houston Movers
If Google is the Coca-Cola of Web search, Bing is RC Cola
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 06-21-2008, 05:46 PM Re: problem editing CSS display attribute
pealo86's Avatar
Webmaster Talker

Posts: 651
Name: Matt
Location: England, north west
Trades: 0
I get ya, thanks. Seems a bit wierd the way it doesnt work, after all; Im sure it worked when I used to use the visibility style?? ah well
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to problem editing CSS display attribute
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.10481 seconds with 13 queries