Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a javascript alert?

I'm definitely not a newbie to scripting, but this just boggles my mind. I want to invoke a function on a button click, so I first wanted to grab the buttonclick event and test that with a simple window.alert. So I just wrote the html document below.

<!doctype html>
    <head>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#sendButton").click(function(){
                    alert("clicked!!");
                });
            });
        </script>
    </head>
    <body>

        <form id="send-message-area">
            <input type="button" id="sendButton" value="Send Message">
        </form>
    </body>

Unfortunately, nothing happens. At all. Since this is fairly simple code, I have no clue why this wouldn't work.

like image 347
kramer65 Avatar asked Feb 10 '26 18:02

kramer65


1 Answers

Your script tag to pull in jQuery is using the (Common|General) Internet Syntax Scheme. This tells the browser to use whatever scheme was used to load the page in loading the script. It is helpful for sites which respond to both http and https.

If you are viewing your file locally using the file:// scheme, then you should see an error that $ is not defined. This is because the script does not live at:

file://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js

Please use this src in the script tag when loading locally:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
like image 168
developerCK Avatar answered Feb 12 '26 15:02

developerCK