7 Replies - 356 Views - Last Post: 27 January 2012 - 04:16 AM

Topic Sponsor:

#1 scottas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-November 11

[Problem] Not working on first click

Posted 26 January 2012 - 03:18 PM

When I click on the link first time ( when page loads ) then nothing hapens. Bud second, third.... times - everything is working. Here is code:
function showHide ( id )
{
	var
		elementas;
		
	elementas = document.getElementById ( id );
	if ( elementas )
	{
		if ( elementas.style.visibility == "hidden" )
		{
			elementas.style.visibility = "visible";
		}
		else
		{
			elementas.style.visibility = "hidden";
		}
	}
	return false;
}


Html:
Body:
<script type="text/javascript" src="jsscriptai.js"></script>

Code:
<div id = "toolbar"><ul><li><a href = "#" onclick = "showHide('loginmenu');">Prisijungti</a></li></ul></div>


What is wrong with it?

Is This A Good Question/Topic? 0
  • +

Replies To: [Problem] Not working on first click

#2 tlhIn`toq  Icon User is online

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3289
  • View blog
  • Posts: 6,893
  • Joined: 02-June 10

Re: [Problem] Not working on first click

Posted 26 January 2012 - 03:24 PM

Instead of this convulted if...else construct couldn't you just toggle the visiblity?



function showHide ( id )
{
	var elementas;
		
	elementas = document.getElementById ( id );
	if ( elementas )
	{
           elementas.style.visibility = !elementas.style.visibility;
	}
	return false;
}

This post has been edited by tlhIn`toq: 26 January 2012 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 scottas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-November 11

Re: [Problem] Not working on first click

Posted 26 January 2012 - 03:42 PM

but then it not works at all
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3289
  • View blog
  • Posts: 6,893
  • Joined: 02-June 10

Re: [Problem] Not working on first click

Posted 26 January 2012 - 03:45 PM

Odd. If you place a breakpoint at line 8 and run this, does the execution stop on this line consistently?
Was This Post Helpful? 0
  • +
  • -

#5 scottas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-November 11

Re: [Problem] Not working on first click

Posted 26 January 2012 - 04:11 PM

I don't know how to use firebug :D. But I see, that javascript thinks, that element visibility is empty ( elementas.style.visibility == "" )
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3289
  • View blog
  • Posts: 6,893
  • Joined: 02-June 10

Re: [Problem] Not working on first click

Posted 26 January 2012 - 05:12 PM

That certainly explains everything!
Was This Post Helpful? 0
  • +
  • -

#7 scottas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-November 11

Re: [Problem] Not working on first click

Posted 26 January 2012 - 11:41 PM

I found out that when I use javascript 'style' when my css styles declared in stylesheet (file style.css) then style of elemwent returns empty string
Was This Post Helpful? 0
  • +
  • -

#8 scottas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-November 11

Re: [Problem] Not working on first click

Posted 27 January 2012 - 04:16 AM

the only way to fix it that I know is:
function showHide ( id )
{
	var elementas;
		
	elementas = document.getElementById ( id );
	if ( elementas )
	{
		if ( elementas.style.visibility == "" )
			elementas.style.visibility = "hidden";
		
		if ( elementas.style.visibility == "hidden" )
			elementas.style.visibility = "visible";
		else
			elementas.style.visibility = "hidden";
	}
	return false;
}


Maybe someone has suggestions?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1