if you have to keep your JavaScript inline, you should use the onclick
attribute instead of using the "javascript:" pseudo-protocol. it was
never fully standardized, and is a lingering piece of old-school
JavaScript usage. so for your example above, use:
<a id="a1" onclick="doSomething();">#a1</a>

an even better solution (as you are already using jQuery), would be to
use the built-in binding jQuery provides, which allows you to a.) keep
your JS out of the markup, and b.) have more than one handler if you
need to:
$(function() {
    $("#a1").click(function() {
        // handle event here
    });
});

On Nov 19, 8:21 am, Jon <jon.klei...@usit.uio.no> wrote:
> Hi,
>
> I have an element like this: <a id="a1" href="javascript:doSomething
> ();">#a1</a>
> However, when I do this: $('#a1').click(); ... then my doSomething()
> do not get called. Why?
>
> If I make my own click function, like this:
> function clickAt(targetElement) {
>         var evt = document.createEvent("HTMLEvents");
>         evt.initEvent("click", "true", "true");
>         return targetElement.dispatchEvent(evt);
>
> }
>
> ... and call it this way: clickAt($('#a1')[0]); ... then my doSomething
> () do get called.
>
> If you like, you can try it out here: <http://folk.uio.no/jkleiser/
> test/jq-test.html>
>
> /Jon

Reply via email to