
January 27th, 2013, 02:42 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 57
Time spent in forums: 11 h 16 m 38 sec
Reputation Power: 1
|
|
Why doesn't this function work by the first click??
Hi there.
I have this JS code to just demonstrate how to show and hide a content by clicking text.
This basically work but only after the second click. When you refresh the page and click on the "Click me please" text, it doesn't seem to fire up the function "changeButton()".
Can anyone explain exactly why, please?
If I can know why, I think I will deepen my understanding of how the JavaScript engine were designed to work.
So please someone help me..
Code:
<script>
function changeButton()
{
var grabHiddenOrShownArea = document.getElementById('hidden_or_shown_area');
if( grabHiddenOrShownArea.style.display == 'block')
{
var displayValue = 'none';
}
else
{
var displayValue = 'block';
}
grabHiddenOrShownArea.style.display = displayValue;
}
</script>
<!-- this is button to fire up the function -->
<div id="button">
<a href="#" id="hit_this_button" onclick="changeButton()">Click me please.</a>
</div>
<!-- shown or hidden area -->
<div id="hidden_or_shown_area">
<h1>Now shown</h1>
</div>
|