jquery - Make radio buttons avoid selection with keyboard navigation? -
is possible make can navigate through set of radio buttons arrow keys without automatically selecting them? have popup want close when user selects radio button , i'm using .click(), when navigate radiobuttons in popup arrowkeys, each navigation leads popup closing , user having reopen it.
suggestions?
thanks!
edit: live demo: jsfiddle.net/tkwzh/4
real thing has lot more open , close function, there way combine keydown enter , mousedown one? like
$("#popup2 input:radio").bind('keydown keycode == 13', 'mousedown' function () {
$('#popup2').css('display', 'none'); });
something this? though realize incorrect , makes no sense.
this behavior specific browsers. ie appears select radios based on arrow keys, whereas, chrome not.
that said, can disable behavior preventing keydown
event via jquery. recommend doing on div (or other element wraps radio buttons), doesn't apply entire document:
$('div.wrap').keydown(function(e) { if (e.which > 36 && e.which < 41) { e.preventdefault(); } var $checkedradio = $('input[type=radio]:checked'); if ($checkedradio.size() > 0) { $checkedradio.removeattr('checked'); if (e.which == 38 || e.which == 39) { var $nextradio = $checkedradio.next('input[type=radio]'); if ($nextradio.size() > 0) { $nextradio.attr('checked', 'checked'); } else { $('input[type=radio]:first').attr('checked', 'checked'); } } else if (e.which == 37 || e.which == 40) { var $prevradio = $checkedradio.prev('input[type=radio]'); if ($prevradio.size() > 0) { $prevradio.attr('checked', 'checked'); } else { $('input[type=radio]:last').attr('checked', 'checked'); } } } });รข
there's way achieve this, , track key pressed, , check in click() event:
var keypressed; $('input').click(function(e) { if (keypressed && keypressed > 36 && keypressed < 41) { e.preventdefault(); e.stoppropagation(); // key pressed during click } else{ // normal click } }); $(document).keydown(function(e) { keypressed = e.which; }); $(document).keyup(function(e) { keypressed = null; });
Comments
Post a Comment