0

I am making a game in javascript and I needed help with the following:

I want to make an image = to a variable then I only want to show the image when I want it too.

So I want the image by default hidden but it is always showing whatever I do. I am only a beginner and I have looked at this page and others on the internet to help me: http://www.roseindia.net/javascript/javascriptexamples/javascript-hide-image.shtml

This is the code so far:

<html> <head></head>
<body>
     <img src="http://wwcdn.weddingwire.com/static/vendor/55001_60000/56375/thumbnails/64x‌​64_SQ_1342622451725-Matangiaerial.jpg"
          id="islandimg">
     <script type="text/javascript">
     var playermoney = 10000;
     var islandarray = new array;
     var car = 500; var watch = 100;
     var diamond = 2000;
     function hideImage() { 
          if (document.getElementById) {
               document.getElementById('islandimg').style.visibility = 'hidden';
          }
     </script>
</body></html>

The code has other stuff in it for the game but ignore it.

8
  • 2
    Please add the code to the question by editing it. Commented Nov 9, 2012 at 21:05
  • I have been trying for 45 mins now I still cant do it I have been looking at different articles still cant do it plz help? Commented Nov 9, 2012 at 21:06
  • You have a function called hideImage but you're never calling it. Commented Nov 9, 2012 at 21:09
  • Do you even execute the hideImage function? (hideImage()) Commented Nov 9, 2012 at 21:09
  • 2
    @Kos- Read meta.stackexchange.com/questions/28416/… and meta.stackexchange.com/questions/5029/… Commented Nov 9, 2012 at 21:13

5 Answers 5

3

There are a few reasons this doesn't work

  1. The Image returns 404
  2. As other stated, array is undefined
  3. Missing ending } after the if statement

Here is a working version in JSFiddle

Here is the JS part with comments

 var playermoney = 10000;
 var islandarray = new Array(); //array was undefined. 
 // a more concise way to write this by the way is: var islandarray = [];
 var car = 500; var watch = 100;
 var diamond = 2000;
 function hideImage() { 
      if (document.getElementById) {
           document.getElementById('islandimg').style.visibility = 'hidden';
      }
 }//Missing end braces 

 hideImage(); //missing call to the function 

By the way, the easiest way to find out of these issues is to look at Chrome / Firefox console, it will complain on things like the array variable issue.

Sign up to request clarification or add additional context in comments.

Comments

2

Looks like syntax errors on your page ..

var islandarray = new array;

supposed to be

var islandarray = new Array(); OR var islandarray = [] ;

Also you never seem to calling the function . you are just declaring it ..

hideImage(); // Call this function in your script 
             //  after taking care of the errors.. 

function hideImage() { 
          if (document.getElementById) {
               document.getElementById('islandimg').style.visibility = 'hidden';
          }
};  -- > Missing this as well

hideImage();  // Then call your function

check Fiddle

2 Comments

Could just as well be simply var islandarray = [];
And also there is a missing ending } for the if statememt
1

You could use jQuery and do this:

function hideImage() {
  $('#islandimg').hide();
}


UPDATE:

An other option would be this:

function hideImage() { 
    document.getElementById('islandimg').style.visibility = 'hidden';
}

or this:

function hideImage() { 
    document.getElementById('islandimg').style.display = 'none';
}

Whats the difference between the last two => http://webdesign.about.com/od/css/f/blfaqhidden.htm

It seams you want to toggle the visible state of the image so you should create a show function too. (By using jQuery you can use the toggle() function for that.)

If you don't want to display the image you could also hide it with CSS by default and then toggle the display state with JavaScript with your showImage() function.

#islandimg {
    visibility: hidden;
}

or

#islandimg {
    display: none;
}

6 Comments

Sure, let's load all of jQuery to hide an image.
True but not helpful. You could also use a hundred other helper libraries but none are actually needed for this task
I thought when he is developing a game this will not be the only js function and with a helper lib like jQuery he maybe can write code that is better readable.
If he's making a game like he says, jQuery will probably be very useful for DOM manipulation.
So would a thousand other libraries. The answer to a JavaScript question is not "use jQuery". Adding such a massive dependency makes this a particularly bad answer to the question as it was asked.
|
1

As a few comments (mine included) are suggesting, you're never actually calling the function. Here's my edit to your script

<html> <head></head>
<body>
     <img src="http://wwcdn.weddingwire.com/static/vendor/55001_60000/56375/thumbnails/64x‌64_SQ_1342622451725-Matangiaerial.jpg"
          id="islandimg">
     <script type="text/javascript">
     var playermoney = 10000;
     var islandarray = new Array();
     var car = 500; var watch = 100;
     var diamond = 2000;
     function hideImage() {
          if (document.getElementById) {
               document.getElementById('islandimg').style.visibility = 'hidden';
          }
         }
// NOW CALL hideImage() TO MAKE IT DO SOMETHING
hideImage();
     </script>
</body></html>

http://jsfiddle.net/rVnfF/

5 Comments

Don't forget the closing brace :)
two issues from original post. 1) new Array(); not new array; and 2) called hideImage()
You Guys Are The Best. I May Not Be Great At Coding But U Are All Great At Helping. Thanks Everyone For Helping Out!
Hi, I was just going to ask if you could help me again. I want the array Island to have the variable of how much it is worth (7,000) and and another var which is the image so later on in the code whenever I wanted to get rid of this item I could. Could you help me with this?
if anyone else could help it would be great
0

The provious posts already answer your question. If you want to call the function where the page loads you need to insert this code:

<body onload="hideImage();">

but correct the other errors:

var islandarray = new Array();

and the final }

otherwise jscript will no load. You should use Firefox with Firebug (or other tools) to check for errors!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.