views:

81

answers:

1

How to use this code as Unobtrusively? To keep content and Behaviour separate.

    <a href="nojavascript.html" onclick="functionName(); return false;" 
onkeypress="functionName(); return false;">Link text</a>
+8  A: 

You could write it like this:

<a href="nojavascript.html">Link text</a>

Then use jQuery's .bind() like this:

$("a[href$='nojavascript.html']").bind("click keypress", function() {
  functionName();
  return false;
});

Or alternatively, give it a class like this:

<a class="noJS" href="nojavascript.html">Link text</a>

And use a .class selector like $("a.noJS") instead to be a bit cleaner (or, if it's unique, use an ID).

Nick Craver
@Nick Craver - I liked your methods
metal-gear-solid
+1 ... ohh thats why you called GEM of SO :)
Richa
@downvoter - care to say why?
Nick Craver