Timmy Timmy - 1 year ago 131
Javascript Question

Uncaught SyntaxError: Unexpected token ) when using void()

I get this error and I've managed to narrow it down to:

<a href="javascript:void();" onclick="myFunction();">aaa</a>

That line of code is now the only thing in my source code and still I get the error in title. Any idea why so?

Even when surrounded with the appropriate HTML elements (html, head, body etc) I am still thrown the error. The error shows up in Chrome dev console and via alert if I include a


function in the head tag. It also occurs when the
method actually exists. As far as I can gather, there is absolutely nothing wrong with that above statement whatsoever.

Answer Source


<a href="javascript:void(0);" onclick="myFunction();">aaa</a>

void expects a parameter.

There's an interesting discussion on using void(0) or other techniques here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download