Your code is correct and it works. The problem is that jsFiddle encapsulates your JavaScript within an onLoad function and then its function gets out of scope for what's in the html.
Change jsFiddle to noWrap
and it works.
Versionworking: link
If you want to use this in a site then you can put the function in the document, wherever you want. Since you are defining a function only, then you do not need to be inside any other function onload
or domready
.
Example: link
If you want to insert this code into a separate file you can add it to the page with
<script src="oMeuScript.js"></script>
If you are in the same directory as index.html
. If you are not have to show the path from root: src="/ < caminho > /oMeuScript.js"