Jump to content
  • Advertisement
Sign in to follow this  
Punkis

[web] JavaScript help request (if...else)

This topic is 4413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there! I've got some problem with a javascript i'm trying to build It looks like this: function visibility() { var visi = document.getElementById('allbox').style.visibility; if (visi == 'visible') {visi='hidden';} else {visi='visible';} } The point of this script is to make a box element hidden when i click the link i insert this javascript into, and when i click the link again i want it to be visible. PROBLEM IS IT ISN'T WORKING AT ALL! AARGGH! :p tried alot of things, but nothing seems to work for me =P The url for the site where the script is located is: http://medieteknik.bth.se/dabj06/index.html The javascript is located at: http://medieteknik.bth.se/dabj06/script.js The CSS is located at: http://medieteknik.btg.se/dabj06/mall.css Thanks for any help!

Share this post


Link to post
Share on other sites
Advertisement
Your variable "visi" is just a plain string, NOT a reference to the visibility property of that element.

You're confused about the semantics of JS. Strings don't behave as if they were passed by reference in this way.

Strings behave like they do in Java (even if they're not implemented the same behind the scenes). You can't replace a reference like that. It won't do what you expect.

Mark

Share this post


Link to post
Share on other sites
The code should work using the following:


function visibility()
{
var visi = document.getElementById('allbox');

if(visi.style.visibility == 'visible')
visi.style.visibility = 'hidden';
else
visi.style.visibility = 'visible';
}



You have to treat the variable, in this case, as an object reference, and as such, to pull and set attributes on the same variable, it has to be a reference, not a value of the reference.

Variable with Object Reference:
var visi = document.getElementById('allbox');

Variable with value of Object Reference:
var visi = document.getElementById('allbox').style.visibility;

It took me a while to catch on to this.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!