I'm trying to create a button that when pressed will display a list of cars that I like (just practising) and then along with that display images of said cars. If the button is pressed a second time or more then I'd like it to toggle on and off.
When I try to run it, the console log shows that the variable is incrementing but not in a way that I'd expect. These are two different ways that I have tried. I've tried some other ways but I'm unable to remember what exactly I've done.
I have an image with the code and the console log reports but due to not having any rep thus far on this site I can't post multiple pics so I'll post a cropped version along with the code below:
And the code:
function counter()
{
//clickCount += 1;
console.log(clickCount + " before");
return clickCount += 1;
}
function displayImages()
{
//console.log(clickCount);
if(counter() % 2 === 1) // if the click count is an odd number
{
console.log(clickCount);
console.log("make images visible");
}
else // otherwise, hide the images.
{
console.log("make images hidden");
}
}
separated:
function counter() {
clickCount += 1;
console.log(clickCount + " before");
return clickCount;
}
function displayImages()
{
//console.log(clickCount);
if(counter() % 2 === 1) // if the click count is an odd number
{
console.log(clickCount);
console.log("make images visible");
}
else if(counter() % 2 !== 1)// otherwise, hide the images.
{
console.log("make images hidden");
}
}
the HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<p id="changeParagraph" onclick="changeColour(), changeText()">If you click this, the paragraph and text colour will change.<br /></p>
<p id="favouriteCars">Click me to see my favourite cars!</p>
<button type="button" id="clickMe" onclick="displayImages(), counter()">Click me</button>
<button onclick="counter()">try it</button>
<img src="https://i.wheelsage.org/pictures/subaru/impreza/autowp.ru_subaru_impreza_wrc_17.jpg" id="cars" style="display: none;"/>
<img src="http://farm8.staticflickr.com/7009/6446244541_e165af10bc_o.jpg" id="cars" style="display: none;" />
<img src="http://www.zastavki.com/pictures/originals/2015/Auto___Mitsubishi_White_sports_Mitsubishi_Lancer_Evolution_IX_097719_.jpg" id="cars" style="display: none;" />
<link rel="stylesheet" href="StyleSheet.css" />
<script type="text/javascript" src="JavaScript.js"></script>
</body>
</html>
If any one could help that would be appreciated. Sorry if I have gone against any of the rules or if duplicated I have tried looking for something similar but couldn't find any info.
Kind regards