JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 27th, 2013, 02:42 AM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 38 sec
Reputation Power: 1
Unhappy 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>

Reply With Quote
  #2  
Old January 27th, 2013, 04:05 AM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,869 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 6 h 19 m 22 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Are you sure that the default value of grabHiddenOrShownArea.style.display is "block"? Really sure?

Reply With Quote
  #3  
Old January 27th, 2013, 06:08 AM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 38 sec
Reputation Power: 1
Well I actually set the value but forgot to put in the question " #grabHiddenOrShownArea{display:block;}" in the CSS or "grabHiddenOrShownArea.style.display == "block""; in the script.


Quote:
Originally Posted by requinix
Are you sure that the default value of grabHiddenOrShownArea.style.display is "block"? Really sure?

Reply With Quote
  #4  
Old January 29th, 2013, 12:53 AM
ankit3010 ankit3010 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 ankit3010 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 8 sec
Reputation Power: 0
Please use this code

Hi,

I have done small changes in your code :-

<script type = "text/javascript">

function changeButton()
{
var grabHiddenOrShownArea = document.getElementById('hidden_or_shown_area');
grabHiddenOrShownArea.style.display= 'none';
</script>

<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" style="display:block">
<h1>Now shown</h1>
</div>


If that area is already available on page load then code is not required for that.
You can easily hide it by writing the above code.
I think the above code contains whatever you want.

Thanks

Reply With Quote
  #5  
Old January 29th, 2013, 01:35 AM
tiwariankit tiwariankit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 5 tiwariankit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 5 m 43 sec
Reputation Power: 0
Reply to previos response

Hi,

I have done small changes in your code

<script type = "text/javascript">

function changeButton()
{
var grabHiddenOrShownArea = document.getElementById('hidden_or_shown_area');
grabHiddenOrShownArea.style.display= 'none';
</script>

<div id="button">
<input type = "button" id="hit_this_button" onclick="changeButton()" value = "Click me please"/></div>

<!-- shown or hidden area -->
<div id="hidden_or_shown_area">
<h1>Now shown</h1>
</div>


Kindly use button instead of hyperlink because hyperlink submits whole page so whenever you click on it the text hide for a moment and again the page get refreshed because of link.
So its better to use button.

Thanks

Reply With Quote
  #6  
Old January 31st, 2013, 12:25 AM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 38 sec
Reputation Power: 1
thanks for your reply.

Well, I wanted the area to be shown when the page is first loaded.
I understand if you set display to none, the toggle function will work from the first click.


Quote:
Originally Posted by ankit3010
Hi,

I have done small changes in your code :-

<script type = "text/javascript">

function changeButton()
{
var grabHiddenOrShownArea = document.getElementById('hidden_or_shown_area');
grabHiddenOrShownArea.style.display= 'none';
</script>

<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" style="display:block">
<h1>Now shown</h1>
</div>


If that area is already available on page load then code is not required for that.
You can easily hide it by writing the above code.
I think the above code contains whatever you want.

Thanks

Reply With Quote
  #7  
Old January 31st, 2013, 12:29 AM
Ihatephp Ihatephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Ihatephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 38 sec
Reputation Power: 1
thanks for your reply.

Well, I wanted the area to be shown when the page is first loaded.
I understand if you set display to none, the toggle function will work from the first click.

But i didn't know using hyperlink is the one that makes the page refresh again when it's clicked. I hated that and thanks.



Quote:
Originally Posted by tiwariankit
Hi,

I have done small changes in your code

<script type = "text/javascript">

function changeButton()
{
var grabHiddenOrShownArea = document.getElementById('hidden_or_shown_area');
grabHiddenOrShownArea.style.display= 'none';
</script>

<div id="button">
<input type = "button" id="hit_this_button" onclick="changeButton()" value = "Click me please"/></div>

<!-- shown or hidden area -->
<div id="hidden_or_shown_area">
<h1>Now shown</h1>
</div>


Kindly use button instead of hyperlink because hyperlink submits whole page so whenever you click on it the text hide for a moment and again the page get refreshed because of link.
So its better to use button.

Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Why doesn't this function work by the first click??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap