Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Function onChange

Tags:

javascript

I have a text input:

<input class="stops" id="stops" onchange="stopsChanged();">

And my current JavaScript looks like:

<script>
  function stopsChanged(){
    alert();
  }
</script>

But when I run my code, I get this error:

ReferenceError: stopsChanged is not defined

Why am I getting this error?

like image 282
Nejthe Avatar asked Feb 23 '26 13:02

Nejthe


1 Answers

You must include your JS file on the page your <input> is on like

<script type="text/javascript" src="path/to/your/file.js"></script>

Edit:

What probably happens is that you include your JS file after the <input> is loaded in the DOM. This way HTML tries to access your function before it's even there. Try to put your <script> in the <head> of your HTML and make sure it isn't in a .ready() function or something similar to it.

Source of above: Uncaught ReferenceError: myFunction is not defined

like image 101
Moduo Avatar answered Feb 25 '26 02:02

Moduo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!