0

I'm pretty sure my error is going to be obvious. I've been working for the last 6 hours and just can't find the brain power to figure where the hell I'm going wrong. If someone can let me know I'd really appreciate it.

Please note, I might commit suicide on finding out my error.

<!DOCTYPE html>

<html>
    <head>
        <script src="https://d10ajoocuyu32n.cloudfront.net/jquery-1.9.1.min.js"></script>
        <script>
            $("#addCompanies").click(function() {
                alert("hello");
            });
        </script>
    </head>
    <body>
        <a id="addCompanies">Continue</a>
    </body>
</html>
1
  • Is your DOM ready to be selected at the time of execution of that JavaScript? Commented Jun 2, 2013 at 23:42

4 Answers 4

4

The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.

<!DOCTYPE html>

<html>
    <head>
        <script src="https://d10ajoocuyu32n.cloudfront.net/jquery-1.9.1.min.js"></script>
        <script>
            $(document).ready(function() {
                $("#addCompanies").click(function() {
                    alert("hello");
                });
            });
        </script>
    </head>
    <body>
        <a id="addCompanies">Continue</a>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

You sir are my saviour. I knew it would have been something stupid. Thanks very much!
1

You need to run this onLoad. Your code is trying to attach the click handler BEFORE the actual DOM element exists.

$(function(){
    $("#addCompanies").click(function() {
        alert("hello");
    });
});

Comments

0

Another method to call a JavaScript function from a click is onclick try:

<script>
function myFunction()
{
alert("Hello World!");
}
</script>
<a id="addCompanies" onclick="myFunction()">Continue</a>

Comments

0

You need to do it inside on dom content ready:

$(function(){
    $("#addCompanies").click(function() {
                alert("hello");
            });
});

http://jsfiddle.net/basarat/RDxSV/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.