I'm sorry I'm sort of... hijacking this thread again
but you guys are better than me at this!
p.s: Keep in mind I'm EXTREMELY new at Javascript (Been messing around for a few minutes)
<!DOCTYPE html>
<html>
<body>
<p>Please input the right code for your cookies:</p>
<input id="cookieGiver" type="text">
<button type="button" onclick="giveCookies()">Get your cookies!</button>
<p id="textCookie"></p>
<script>
function giveCookies() {
var val = document.getElementById("cookieGiver"
.value;
var elem = document.getElementById("textCookie"
;
var counter = document.getElementById("counter"
;
var cookieCounter = 0;
So the problem here is that var cookieCounter doesn't go up if I input "Gib" after having done it once. Although if I input an invalid code, the program still detects I did and changes "elem" as such.
What's going on
25-Jun-2014 02:44:40
- Last edited on
25-Jun-2014 02:45:08
by
Morgan
Amaethwr http://www.indecentcode.com/rslinkify_images/10442.png 10442
Hiya Amaethwr, Everything is good here and it's nice to see you found your way into Off Topic
@Morgypie, this may not be the best way to do it, but it's easy to see how different variables work in this example. Just one little line of code needs to be moved
http://puu.sh/9IVG8.png
Each time your function runs, the counter is being declared, assigned zero then has 50 added to it (so it will always be 50). By declaring the counter before the function makes it global and means it can used, increased, decreased, etc, without having to re-declare it.
Basically now when the page loads, counter is now declared before your function has run. Your function will use the last known value of the counter and increment that by 50
25-Jun-2014 04:04:32
- Last edited on
25-Jun-2014 04:30:50
by
Indecent Act