3

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?

asked Feb 27, 2015 at 9:03
10
  • It is working perfectly fine at my end. It might be you have written some other script above this function which is not valid Commented Feb 27, 2015 at 9:07
  • The code alone should work, assuming it is all in the same page? Commented Feb 27, 2015 at 9:07
  • no it's not in the same page. My javascript code is on anther page. Commented Feb 27, 2015 at 9:09
  • then it won't work... the script and element must be there in the same page Commented Feb 27, 2015 at 9:10
  • Make sure your js file and html file in the same location. And you should inlcude the js file in your html file Commented Feb 27, 2015 at 9:11

3 Answers 3

1

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

answered Feb 27, 2015 at 9:12
Sign up to request clarification or add additional context in comments.

3 Comments

@Nejthe I did more research and edited my answer according to what I've found.
There is no need to use an external file. There is no need to use a type attribute. Including the JS after the input doesn't matter - the HTML won't try to access the function until the event handler is fired.
Check the source I've added before jumping to conclusions.
0

try it this way.

HTML:

<input id="stops">

JS:

document.getElementById('stops').onchange = function(){
 console.log('test'); 
}

It's also better in terms of separation of function and presentation.

answered Feb 27, 2015 at 9:15

Comments

0

I think your problem is that you have HTML tags inside of your external javascript file. You do not need to wrap your external javascript code with html tags in the contents of the .js file.

In your HTML file though, make sure you define your script type in the opening tag if you're using an external file reference:

<script type="text/javascript" src="http://www.yoursource.com/file.js"></script>

In some browsers, it will not know how to parse the external resource without the type being defined in the opening tag.

answered Feb 27, 2015 at 9:09

4 Comments

Script type is optional and by default equal to text/javascript
@aduch, If the script is defined within the brackets, then you are right. However, not if the script resource is external via a 'src' attribute in the script tag.
You are right for HTML<= 4 or (x)HTML, but not for HTML5. Anyways, there is rarely too much information about these kind of answers
@Nejthe, The only thing I can think of is improper syntax in one of the other functions above your stopsChanged() function definition. This would be an unclosed/open bracket somewhere that is breaking the browser when it parses the external file.

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.