/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] CSS assignments in JS

When, exactly, do the parameters of a CSS style get assigned to the element?
In the following simple example, I have 2 buttons that control the display of 2 <DIV> areas.

The initial display is as expected: 2 buttons one <DIV> section
All works as expected when the ‘SHOW’ is clicked, even though the initial value of display is ”

When you click the ‘HIDE’ button, nothing happens the first time because the alert returns ”
even though the CSS assignment was ‘none’. Two clicks latter, both work as expected.

When do the CSS class assignments get recognized as it appears in this case that the
display:none is not being recognized until after the JS assignment change?
I don’t understand why the alter on the FIRST click doesn’t return a ‘preSHstatus’ setting
of the initial assignments of the CSS definitions.

Hopefully, I’m making myself clear about this question.
Here’s the code:

[code=php]
<html>
<head>
<title>CSS test</title>
<style type=”text/css”>
#initHide {
display: none;
width:100px;
border:1px solid green;
}
#initShow {
display: block;
width:100px;
border:1px solid red;
}

</style>
<script type=”text/javascript”>

// Toggle.js

function Toggle(Info) {
var CState = document.getElementById(Info);
var preSHstatus = CState.style.display;
if (CState.style.display == “none”) { CState.style.display = “block”; }
else { CState.style.display = “none”; }
var postSHstatus = CState.style.display;
alert(‘Pre status : ‘+preSHstatus+”nPost status: “+postSHstatus);
}

</script>
</head>
<body>
<button onclick=”Toggle(‘initShow’)”>Init Show</button>
<button onclick=”Toggle(‘initHide’)”>Init Hide</button>
<div class=”divS” id=”initShow”>Initially Show</div>
<div class=”divH” id=”initHide”>Initially Hide</div>

</body>
</html>
[/code]

Thanks for any interest.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMay 20.2008 — You'll actually want to add [B]style="display:none;"[/B] to the DIV tag that should be hidden.

Alternately, I wrote a JavaScript class to handle toggle boxes, which you might be interested in.

[upl-file uuid=316a1291-8f04-46f7-be48-95809af21cc3 size=3kB]ToggleGroup.zip[/upl-file]
Copy linkTweet thisAlerts:
@JMRKERauthorMay 20.2008 — 
You'll actually want to add [B]style="display:none;"[/B] to the DIV tag that should be hidden.
[/QUOTE]


Yes, I could do that, and will if necessary,

but I thought that was the purpose of the CSS style definitions.

What am I thinking wrong about this?

Thanks for the reply.
Copy linkTweet thisAlerts:
@bluestartechMay 20.2008 — you would need a div

<div id="initHide">

to hide the div to start with, but you do not need to set a display style property at the div level
Copy linkTweet thisAlerts:
@JMRKERauthorMay 20.2008 — you would need a div

<div id="initHide">

to hide the div to start with, but you do not need to set a display style property at the div level[/QUOTE]

I do have that in the original post.

I also experimented with this, but there was no effective change.
[code=php]
<style type="text/css">
divH,
#initHide {
display: none;
width:100px;
border:1px solid green;
}
divS,
#initShow {
display: block;
width:100px;
border:1px solid red;
}

</style>
[/code]

I can also do this to make it work correctly, but I don't understand why I should have to. ?
[code=php]
if ((CState.style.display == "none") || (CState.style display != '')) {
CState.style.display = "block";
} else {
CState.style.display = "none";
}
[/code]


It looks to me that the 'border' and 'width' are assigned correctly in the <STYLE> region

but that the 'display' parameter is not!

I'm not trying to be a pest, but I just don't understand why this is happening or if I am assigning things incorrectly!
Copy linkTweet thisAlerts:
@ChazzlMay 20.2008 — Im my experience, the CSS display property must be present in the physical element you want to control... not just in the CSS declaration in the <head> tag. Example:
[code=html]<div class="divS" id="initShow">Initially Show</div> [/code]

Needs to be:
[code=html]<div class="divS" id="initShow" style="display:block;">Initially Show</div>[/code]


[code=html]<html>
<head>
<title>CSS test</title>
<style type="text/css">
#initHide {
display: none;
width:100px;
border:1px solid green;
}
#initShow {
display: block;
width:100px;
border:1px solid red;
}
</style>
<script type="text/javascript">
function Toggle(Info) {
var CState = document.getElementById(Info);
var preSHstatus = CState.style.display;
if (CState.style.display == "none"){
CState.style.display = "block";
}else{
CState.style.display = "none";
}
var postSHstatus = CState.style.display;
}
</script>
</head>
<body>
<button onclick="Toggle('initShow')">Init Show</button>
<button onclick="Toggle('initHide')">Init Hide</button>
<div class="divS" id="initShow" style="display:block;">Initially Show</div>
<div class="divH" id="initHide" style="display:none;">Initially Hide</div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@JMRKERauthorMay 20.2008 — Im my experience, the CSS display property must be present in the physical element you want to control... not just in the CSS declaration in the <head> tag. Example:
[code=html]<div class="divS" id="initShow">Initially Show</div> [/code]

Needs to be:
[code=html]<div class="divS" id="initShow" style="display:block;">Initially Show</div>[/code][/QUOTE]


If that is the case, I can live with it ...

but is it only true for the 'display' or are there other parameters I should also set-up this way?

Also, if it is true, should I also include the width and border definitions inline also?

As in:
[code=html]<div class="divS" id="initShow" style="display:block;width:100;border:1px solid red">
Initially Show</div>[/code]

And when I do, do I still nee to define in the <STYLE> section (or externally linked CSS file)?

Bottom line, I still don't understand when to define in-the-element

and when to define in-the-<STYLE> area. Are there any rules about this?

I appreciate you all for putting up with my lack of knowledge on this subject.

I just don't know why and/or when to be aware of the CSS settings and that's what's bothering me.
Copy linkTweet thisAlerts:
@ChazzlMay 20.2008 — I only run into that problem with the css display property... border, width, and other styles work without the inline css. Im not sure why.
Copy linkTweet thisAlerts:
@JMRKERauthorMay 20.2008 — @Chazzl, thanks. I'm not sure why either, but what will be ... will beeeee.

Just another 'feature' I don't fully understand.

And to all who responded to this thread, thanks again!
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...