• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Script not working?

Status
Not open for further replies.
Level 17
Joined
Dec 11, 2014
Messages
2,004
Hey there hive! I Hope you have a nice day.
It's the second day I'm trying to learn some ways to write scripts, So I'm a n00b.
Anyways, on one of my JavaScript Scripts, I encountered a Problem:

The main Idea is supposed to be Something like Hidden tags, To show screenshots when a button is clicked, and Hide when clicked again. I Used some variables and If/then/elses, they didn't work out... So any help would be appreciated.


The button things
Code:
<button onclick="displayScreenshots()">Screenshots</button>
The script:
Code:
function displayScreenshots() {
    var boolScreenies = "false";
    var ScreenshotString;
    if (boolScreenies = false) {
         boolScreenies = "true";
         ScreenshotString = "Daiee"
    } else {
         boolScreenies = "false";
         ScreenshotString = "";
    }
    document.getElementById("ScreenshotsHere").innerHTML = ScreenshotString;
}



And don't hurt me, I'm a noob :D

I also tried inspecting the Hidden tags in the Hive, understood Nothing :(
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
You have three different errors.

First of all, you are mixing the assignment operator (=) and the equality operator (==, but you'd usually want to use ===, to be extra safe).

The second issue is that you are using a string, but comparing it to a boolean.
"false" is a string, false is a boolean.

Finally, the last error is a logical one - you are checking if a variable you just set at the beginning of the function call is a certain value.
You just set it - the value will always be "false".
You probably meant to use a global variable, just declare it outside of the function.

With all of these changes, it should be something like this:
JavaScript:
var boolScreenies = false;

function displayScreenshots() {
    var ScreenshotString;
	
    if (boolScreenies === false) {
         boolScreenies = true;
         ScreenshotString = "Daiee";
    } else {
         boolScreenies = false;
         ScreenshotString = "";
    }
	
    document.getElementById("ScreenshotsHere").innerHTML = ScreenshotString;
}
 
Status
Not open for further replies.
Top