JavaScript Uncaught ReferenceError: [functionName] is not defined uncaught referenceerror functionname is not defined

Sincere apologies if this has already been covered. I've been trawling StackOverflow for a while now and I cannot find anything that will fix my issue. Many people have this error, but theirs seem to be derived from syntax errors and the suchlike. I (nor JSLint) can't find any such errors, so I'm rather stumped. The following code (from the W3Schools page) does work:

<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World";
}
</script>

However, my code does not work:

  <head>
  <!--SNIP-->
  <script language="text/javascript">
    function iconclicked() {
      alert("Yay!");
    }
  </script>
  </head>
  <body>
    <div class="icon" id="misc" onclick="iconclicked()"></div>
  </body>

This produces the "Uncaught ReferenceError: iconclicked is not defined" error. But, and correct me if I'm wrong, function iconclicked() {...} is what defines the function and it is clearly present. Additionally, moving the JScript to be within the <body>tag does not fix the problem, regardless of whether to script is before or after the div. If you were to substitute the div for another element (a button, image, text, etc) or to call the function a different way (e.g. via onload) the code does not work.

I thought that perhaps it was the rest of the code on my page causing something funny to happen, so I headed over to JSFIDDLE and popped the old W3Schools code in there. Lo and behold, "Uncaught ReferenceError: myFunction is not defined". JSFDDLE link Edit: turns out this is just due to the fact I didn't choose 'no wrap' in the JSFIDDLE options - thanks Peter Healy :)

But if you go ahead put the JS inside <script> </script> tags in the HTML section of JSFIDDLE, there is no such problem and the code works as intended. JSFIDDLE link

I figure maybe this is a scope problem, but I can't for the life of me work out what it is specifically. Hopefully someone out there with more experience has the answer!

Answer:1

There is no language of "text/javascript", that is the type.

Change

<script language="text/javascript">

to just

<script>

Do not worry about the language or type attributes. Language has been deprecated.

Answer:2

It's actually <script type="text/javascript">, with a type attribute, not language:

  <head>
  <!--SNIP-->
  <script type="text/javascript">
    function iconclicked() {
      alert("Yay!");
    }
  </script>
  </head>
  <body>
    <div class="icon" id="misc" onclick="iconclicked()"></div>
  </body>

The language attribute went out of style a while ago.

Answer:3

I need to draw a line in the following manner:   For now, it will be only drawn in code, no user input. My question is, how to draw perpendiculars to a line, if I draw it point by point? (Obviously,...

I need to draw a line in the following manner:   For now, it will be only drawn in code, no user input. My question is, how to draw perpendiculars to a line, if I draw it point by point? (Obviously,...

  1. drawing line with bearing and distance autocad
  2. drawing line with angle autocad
  3. drawing line with coordinates in autocad
  4. drawing line with arrow
  5. drawing with line
  6. line drawing with color
  7. lined paper drawing
  8. line drawing with shading
  9. line drawing with photoshop
  10. line drawing with pen
  11. line drawing with gimp
  12. line drawing with illustrator
  13. drawing with line of action
  14. line drawing with dda
  15. line drawing with mouse
  16. line drawing with hand
  17. drawing with line tool
  18. drawing a line with css
  19. drawing a line with a chicken
  20. drawing a line with a compass

Code: $("input[type=checkbox]").on("click", function() { deptsSelected = '<div id = "selected"> ' + $("label[for='" + this.id + "']").text() + '</div>'; $("#tabs").append(...

Code: $("input[type=checkbox]").on("click", function() { deptsSelected = '<div id = "selected"> ' + $("label[for='" + this.id + "']").text() + '</div>'; $("#tabs").append(...

I have the following JS: "a a a a".replace(/(^|\s)a(\s|$)/g, '$1') I expect the result to be '', but am instead getting 'a a'. Can anyone explain to me what I am doing wrong? Clarification: What I ...

I have the following JS: "a a a a".replace(/(^|\s)a(\s|$)/g, '$1') I expect the result to be '', but am instead getting 'a a'. Can anyone explain to me what I am doing wrong? Clarification: What I ...

  1. string replace only first instance c#
  2. string replace only first instance java
  3. string replace only first instance javascript
  4. string replace only first instance
  5. string replace only first instance python
  6. string.replace only first occurrence
  7. string.replace only one occurrence java
  8. string replace only first instance php
  9. string replace only first occurrence java
  10. string replace only first occurrence php
  11. string.replace only one occurrence c#
  12. string replace only once python
  13. string replace only
  14. python string replace only first occurrence
  15. js string replace only first
  16. javascript string replace only replaces first
  17. python string replace only last occurrence
  18. javascript string replace only first match
  19. c# string replace only last occurrence
  20. string replace javascript only once

Here is the problem, I need to create an image file with a .svg file. I have a function which should draw the svg, and then, i get it from a canvas to save it as an image file. my draw function is : ...

Here is the problem, I need to create an image file with a .svg file. I have a function which should draw the svg, and then, i get it from a canvas to save it as an image file. my draw function is : ...

  1. image onload firing
  2. image.onload not working
  3. image onload not working chrome
  4. image onload not working ie
  5. image onload doesn't work
  6. image onload javascript not working
  7. image onload function not working javascript
  8. image onload function not working
  9. jquery image onload not working
  10. canvas image onload not working
  11. firefox image onload not working
  12. react image onload not working
  13. safari image onload not working
  14. js image onload not working
  15. javascript image onload not working ie