0

I am coding a taximeter which is calculating the drive-expenses through the elapsed time. By clicking a button the standing below function "startDrive" will normally executed, but when I am clicking on my button the browser or console respectively is showing an error which is including the message "Uncaught ReferenceError: startDrive is not defined at HTMLButtonElement.onclick (time.html:15:62)". I checked whether I mentioned the wrong Function in the "onclick" part in HTML but this is not the case. What I am doing wrong?

//global variables
let y = 3.9;
let reversal = 20;
//Main-function in which the sub-functions are be executed
function startDrive() {
 document.getElementById('output1').innerHTML = y.toFixed(2) + "€";
 interval();
}
//Calculating drive-expenses
function calculate() {
 if(y < reversal) {
 y += 0.14375;
 }
 else if(y > reversal) {
 y += 0.103125;
 }
 document.getElementById('output1').innerHTML = y.toFixed(2) + "€";
 
}
//Fixed time in which "calculate()" will be executed
function interval() {
 timerId = setInterval(calculate, 5000);
}
//Stopping interval
function stop() {
 clearInterval(timerId);
}
<button onclick = "startDrive()">start!</button>
<output id = "output1"></output>
asked Aug 6, 2022 at 14:09
8
  • The code doesn't show how you attach the function to the button click? Commented Aug 6, 2022 at 14:13
  • We don't have enough information to help here. Please make it a minimal, reproducible example. Commented Aug 6, 2022 at 14:14
  • Make sure you call the method not a value or property like: <button onclick="startDrive()">Click me</button> not <button onclick="startDrive">Click me</button> Commented Aug 6, 2022 at 14:14
  • @evolutionxbox I uploaded the HTML part Commented Aug 6, 2022 at 14:23
  • @yousoumar I uploaded the HTML part Commented Aug 6, 2022 at 14:23

2 Answers 2

1

here

<button onclick = "startDrive">start!</button>

use startDrive() instead of startDrive

you can watch a sample at here.

answered Aug 6, 2022 at 14:27
Sign up to request clarification or add additional context in comments.

2 Comments

In my actual code i wrote my code like your suggested. So even in this way the function "startDrive()" is not defined according to the browser
sorry , I cant replay your problem,your code runs perfectly on my side,this is my sourcecode.
0

It says startDrive is not defined, so it seems your function definitions are never executed.

If you put your code in a plain script tag inside head it works just fine:

https://codesandbox.io/s/great-joana-ubttg5?file=/index.html

answered Aug 6, 2022 at 14:41

3 Comments

Although I agree with you, this wouldn't solve the issue because (unless the JS never ran) it would have executed by the time the OP clicked the button.
"it seems your function definitions are never executed." Why is that so
If they were, startDrive would be defined. So either they're never run or they're not run in the global context. You'll have to elaborate on your setup. This is just guesswork. It generally works, as you can see in the sandbox.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.