-3

I'm programming a basic click counter for my site on a hidden Easter egg page when I encountered a problem I never had when programming web before: Does Javascript have an equivalent to other programming languages goto. The code is below, if anyone can make adjustments to it so that the displayed "clicks" are altered and do not remain at 0 while the variable itself if changed later in the code.

<html>
<body>
<h2>
<script>
var clicks = 0
</script>
<script>
document.write(clicks)
</script>
<br>
<button onclick ="clicks = clicks + 1">Click me</button>
</body>
</html>
Tristan
  • 27
  • 6
  • 3
    Updating text fields is not usually done with goto in any language. – Chuck Apr 26 '14 at 23:12
  • It's really not clear what you're trying to achieve or how you think a `goto` will help. – p.s.w.g Apr 26 '14 at 23:13
  • There are a crazy number of topics which use your exact same question heading: http://stackoverflow.com/search?q=Javascript+goto – Francis Smart Apr 26 '14 at 23:13
  • @Don Roby: I think that situation is different here. Though questions appears to be same. – DRAX Apr 26 '14 at 23:21
  • Of course [JavaScript has a goto equivalent](http://summerofgoto.com/). – nnnnnn Apr 26 '14 at 23:30
  • Consider making a specific question instead of asking to modify your code. Also it is not clear how the goto statement will help with this. You might prefer to define a function and call it whenever you want to execute certain code instead of altering the flux of execution with a goto. – Jorge Rivera Jul 26 '19 at 15:16

4 Answers4

0

What you want is to write and then repeatedly call a function. And you don't want that function to call document.write; you probably want it to append text to an existing DOM node.

I suggest picking up an introductory book on JavaScript.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

No. Generally using goto statement is bad idea.

What you need to do is to write clciks to specific element each time when user clicks. Like this:

<html>
<body>
<h2>
<script>
var clicks = 0
</script>
<div id="myinfo"></div>
<br>
<button onclick="clicks = clicks + 1; document.getElementById('myinfo').innerHTML=clicks">Click me</button>
</body>
</html>

Also, you could use shorter command:

<button onclick="clicks = clicks + 1; myinfo.innerHTML=clicks">Click me</button>
DRAX
  • 30,559
  • 2
  • 26
  • 28
0

Create the following html content:

    <div id="myDiv"></div>

Also, after updating the clicks variable value, update the div content like this:

document.getElementById("myDiv").innerHTML = clicks;
Edge7
  • 681
  • 1
  • 15
  • 35
0

Although I'm a novice at Javascript, I have experience with Python, Java and C++ and the only real GOTO alternative is a user defined function. In javascript, I believe it is just function functionname { code here }

Beta Decay
  • 805
  • 1
  • 8
  • 20