views:

327

answers:

1

Is anybody able to -execute- a generated Ajax.ActionLink when a user presses a key on the keyboard? (This is needed for accessibility)

NOTE: I'm using ASP.NET MVC + Microsoft Js libraries (...Ajax.js / ...MvcAjax.js) + jQuery

Javascript to capture keypress (IE + Firefox)

$(document).keypress(function(event) {
    if(event.keyCode == 27) {
        //execution here
        var a = document.getElementById('linkid');
    }
});

Html generated by ASP.NET MVC (Ajax.ActionLink())

<a id="linkid" href="/controller/action" onclick="
Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), 
{ insertionMode: Sys.Mvc.InsertionMode.replace, 
updateTargetId: 'SomeDivId' });
">LinkText</a>


The following is not what i'm looking for, this doesn't work!

$(document).keypress(function(event) {
    if(event.keyCode == 27) {
        var a = document.getElementById('linkid');
        a.onclick();           //doesn't exist in Firefox
        a.click();             //doesn't "work" in Firefox (reference to "this" [a] is needed .NET MVC javascript)
        a["onclick"]();        //same as .onclick()
        a["click"]();          //same as .click()

        //or even:
        a.onclick.apply(a);    //doesn't exist in Firefox
        a.click.apply(a);      //Somehow keeps "this" reference, but throws Sys.ArgumentUndefinedException
    }
});
+2  A: 

Have you tried using jQuery's trigger mechanism?

$(document).keypress( function(e) {
    if (e.keyCode == 27) {
       $(this).trigger('click');
    }
}

Failing that, you could just invoke the href, which will do a full postback, but should accomplish the desired action if the action is written to handle both AJAX and non-AJAX requests.

$(document).keypress( function(e) {
    if (e.keyCode == 27) {
       location.href = $(this).attr('href');
    }
}
tvanfosson
Gosh, I haven't tried the jQuery part. I think I might actually get it working. For the second part: I've created handlers for both AJAX and non-AJAX requests, however it's an intranet and everybody (IE7) and me (FF) should get it working with AJAX, especially using keyboard input. I'm gonna try the jQuery part next thing tomorrow.
Ropstah
Well potverdrie, it works! :)
Ropstah