-3

I have an html document in which a function is called like this <div id="mon" class="topbar" onclick="verd()"> This calls a jquery function inside the body tag that goes like this

<script src="jquery.js">
  function verd(){
    $("#stillmain").empty();
  }  </script>

This returns the error:

Uncaught ReferenceError: verd is not defined
at HTMLDivElement.onclick

When I change the script to pure Javascript without Jquery however like this

  <script>
  function verd(){
    $("#stillmain").empty();
  }  </script>

it does recognize the function verd(), can anyone explain what is going on, and how I could still use Jquery? I am using the newest version of Jquery(just changed the name, nothing else).

JFugger_jr
  • 313
  • 1
  • 4
  • 13

2 Answers2

5

When you give a script tag a src attribute it's going to load that source file in place of the script contents. So any script tags with a src should be kept empty.

You need to load jQuery by calling a script tag with its source on it, and then in a separate tag you can make your own script tag and write your custom JavaScript there.

<script src="jquery.js"></script>
<script>
  function verd(){
    $("#stillmain").empty();
  }
</script>

Make sure that your custom scripts are loaded after jQuery.

glhrmv
  • 1,712
  • 1
  • 16
  • 23
0

If you want to run this as a jQuery function you need to use the proper syntax. Try the link below.

How to Create a jQuery Function

Also, you need to load jQuery in a separate tag or else none of your code will be ran other than the source of that script!

JustADrone
  • 18
  • 3