Matheus Matheus - 12 days ago 6
Javascript Question

Very basic jQuery code not working

I was programming on jQuery, and I was met by a page with a strange behavior: very basic jQuery/javascript functionality simply not working. I have the mentality of "if it isn't working, then it's my fault and never the programming language's ", but apparently in this case this is not quite the truth. This is the code:

<html>
<head>
</head>
<body>


<script>

$(document).ready(function(){

$("#id3").click(function(){

alert("ada");

});

});

</script>

<button type="button" id="id3">id</button>

</body>
</html>


This code is in a page on my localhost, which has several other pages with working jQuery functions much MUCH more complicated than that. I honestly have no clue why or how would this simple code NOT work.

Any insight is appreciated

Answer

You need to reference the JQuery library with a script tag. Without it, the special syntax and objects provided by the library won't be known to your browser.

$(document).ready(function(){

        $("#id3").click(function(){
            alert("ada");
        });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="id3">id</button>