Your code is correct and it works. The problem is that jsFiddle encapsulates your Javascript within an onload function and then your function is out of scope for what’s in html.
Switch to jsFiddle noWrap
and it already works.
If you want to use this on a website then you can put the function in the document, wherever you want. Since it is defining a function only, then it does not need to be within any other function onload
or domready
.
Example: http://jsfiddle.net/2304rpnf/3/
If you want to insert this code into a separate file you can add to the page with
<script src="oMeuScript.js"></script>
in the event of being on the same board as the index.html
. If you are not have to show the path from root: src="/ < caminho > /oMeuScript.js"