1

I have - what I think - should be a very simple jquery script. When the register_hyperlink anchor is clicked, I am presented with the alert box, as expected; but after I click the OK, I am getting an error: Uncaught SyntaxError: Unexpected token )

The code block is below. I can't see anywhere that there are unbalanced parenthesis as the error states. The code below is inside the element of my html page.

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.1.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $("#register_hyperlink").click(function() {
      alert('Hello');
     });
  });
</script>

Does anyone have any idea how to go about debugging this? I've been at it for a while now, but am having zero luck.

Thanks!

5
  • 2
    The console error log will have some details about the source of the error and a stacktrace can you share them too? is there a onclick attribute in the a element Commented Jun 5, 2013 at 3:42
  • 2
    That code is fine. The problem is elsewhere. Commented Jun 5, 2013 at 3:42
  • Only thing i can think of is trying .on('click', function(){ instead, you are using the most recent jquery lib , maybe .click is deprecated by now Commented Jun 5, 2013 at 3:44
  • in jsfiddle works perfectly Commented Jun 5, 2013 at 3:44
  • @JayRizzi No, .click() isn't deprecated...a simple check in the jQuery docs would confirm that. It's just a shortcut for .on("click". And being deprecated doesn't mean it's not available, it means it will be removed in a future build. Commented Jun 5, 2013 at 3:51

3 Answers 3

5

I had:

<a id="register_hyperlink" href="javascript:void();">Register an account</a>

I changed it to:

<a id="register_hyperlink" href="javascript:void(0);">Register an account</a>

So that explains it :-)

Sign up to request clarification or add additional context in comments.

Comments

1

What have you got in the href attribute of your anchor? Alternatively do you have a an onClick attribute in the anchor or are you catching it anywhere else?

You are not preventing the default behaviour of the anchor and you may have a syntax error in the href. That would be my first guess.

You could change your posted function to:

$(document).ready(function () {
    $("#register_hyperlink").click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        alert('Hello');
    });
});

If you test with this function you may find, as Matt Ball points out that your problem was indeed elsewhere

Comments

1

you could use

<a id="register_hyperlink" href="javascript:;">Register an account</a>

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.