ASP.NET MVC Ajax - Is it possible to execute a -GENERATED-Ajax link with keyboard input?
Can anyone have an -execute - generated Ajax.ActionLink when user presses a key on the keyboard? (This is required for accessibility)
NOTE. I am 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>
Not what I'm looking for, it 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
}
});
0
a source to share
2 answers
Have you tried using jQuery's launch mechanism?
$(document).keypress( function(e) {
if (e.keyCode == 27) {
$(this).trigger('click');
}
}
Otherwise, you can just call the href, which will do the full postback, but should perform the desired action if the action is being written to handle both AJAX and non-AJAX requests.
$(document).keypress( function(e) {
if (e.keyCode == 27) {
location.href = $(this).attr('href');
}
}
+2
a source to share