Brett
Brett

Reputation: 12007

jQuery - Uncaught SyntaxError: Unexpected token )

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!

Upvotes: 1

Views: 5658

Answers (3)

dekarvn
dekarvn

Reputation: 78

you could use

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

Upvotes: 1

Brett
Brett

Reputation: 12007

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 :-)

Upvotes: 5

dcarson
dcarson

Reputation: 2853

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

Upvotes: 1

Related Questions