Prevent anchor navigation with click binding

1,131 views
Skip to first unread message

Adam Pawsey

unread,
Sep 11, 2013, 7:18:26 PM9/11/13
to knock...@googlegroups.com
Hi guys,

I'm trying to find a way to have an attr (href) binding and a click binding on the same anchor tag, and have the function bound to the click to decide whether navigation is allowed.

in plain html this would be something like <a href='/' onclick='javascript return decideNavigation()' >Something </a>
In that case if the the function decideNavigation returned true, the navigation to '/' would occur and if it was false, the navigation would be prevented.

Any simple way to do this with knockout or would it require something like a custom binding?

Thanks.

Patrick Steele

unread,
Sep 11, 2013, 9:09:49 PM9/11/13
to knock...@googlegroups.com
See note #3 at http://knockoutjs.com/documentation/click-binding.html

"By default, Knockout will prevent the click event from taking any default action. This means that if you use the click binding on an a tag (a link), for example, the browser will only call your handler function and will not navigate to the link’s href. This is a useful default because when you use the click binding, it’s normally because you’re using the link as part of a UI that manipulates your view model, not as a regular hyperlink to another web page.

However, if you do want to let the default click action proceed, just return true from your click handler function."

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages