user1670407
user1670407

Reputation:

jQuery button click wont run

I cannot get this simple script to run and been staring at it for awhile now and don't see any syntax errors. Simply want the alert to show on click.

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type='text/javascript'>

$(document).ready(function() {
    $("#authenticate_button").click(function() {
        alert("click");
    });
});

</script>

</head>
<body>
    <input id="code" type="text"> 
    <button id='authenticate_button'>Authenticate</button>
</body>
</html>

Upvotes: 1

Views: 230

Answers (2)

palaѕн
palaѕн

Reputation: 73896

You can also try out with this:

Using jQuery's CDN provided by MediaTemple

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

Using Microsoft's CDN

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>

as an alternative to jQuery js file.

I would suggest to go for this:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>  
<script>  
     // Fallback to loading jQuery from a local path if the CDN is unavailable  
     (window.jQuery || document.write('<script src="/scripts/jquery-1.9.0.min.js"><\/script>'));  
</script>

Upvotes: 0

Blender
Blender

Reputation: 298076

You're opening the file with your web browser directly. // is shorthand for your current protocol (which is file://), so jQuery isn't loading from Google's CDN.

You need to explicitly specify the protocol by adding http: before // in this line:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

I'd also add a doctype:

<!DOCTYPE html>

Upvotes: 6

Related Questions