If you are using code that uses jQuery, it needs to be loaded before any other feature that uses it as a dependency.
In JSFiddle , this seems irrelevant since you just need to import a resource and that's it, the code in the Javascript field works . It turns out that beneath the wipes, before running the Javascript block, all features are preloaded.
And that's the way your code should be, by importing the resources first (in this case, jQuery) and then making use of their functions.
Quite simply, this code will work as an example:
<!-- Importando a biblioteca -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script><!--FazendousodojQuerydepoisdeimportado--><script>$(function(){$('body').text('Olá!');});</script>
ThefollowingcodewillnotworkbecausejQuery(or$
)isundefined.Evenifyourunthesnippetandopenthebrowserconsole,you'llseeareferenceerrorlikethis:
ReferenceError:$isnotdefined.
<!-- Fazendo uso do jQuery antes de importado -->
<script>
$(function(){
$('body').text('Olá!');
});
</script>
<!-- Importando a biblioteca -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<!doctype html>
<html>
<head></head>
<body>
<div id="DivOne" class="oddNum">One</div>
<div id="DivTwo" class="evenNum">Two</div>
<div id="DivThree" class="oddNum">Three</div>
<button id="btnOne">Reset odd numbers</button>
<button id="btnTwo">Reset even numbers</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script><script>$(function(){//$(function(){});éomesmoque$(document).ready(...)$('.oddNum').css('background-color','#DEA');$('#DivTwo').css('background-color','#FCC');$('#btnOne').click(function(){//Actiongoeshere$('.oddNum').css('background-color','#FFF');});$('#btnTwo').click(function(){//Actiongoeshere$('#DivTwo').css('background-color','#FFF');});});</script></body></html>