Shaun Mccran

My digital playground

21
S
E
P
2010

Simple JQuery method of intercepting a hyperlink

Ever wanted to run a custom routine (like validation) on a template when a user clicks a link to proceed to the next step? I recently worked on a checkout template that was not a form, but still needed some validation installed when a user clicked the 'proceed' option. The code below shows a very simple way of using a JQuery selector to intercept the href click event and replace it with a custom function. My example shows a 'warning' div, and then returns false, IE does not action the click URL request.

view plain print about
1<s/cript type="text/javascript">
2$(document).ready(function(){
3$("#element").click(function() {
4// Do some custom validation - show a div
5$(".warning").show();
6
7// disable the click
8return false; });
9});
10</script>
11
12<a href="" id="element">proceed</a>
Normally I'd use the validation JQuery plugin, but in this case this page isn't even a form, its just an overview template.
TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top