Skip to content Skip to sidebar Skip to footer

How To Skip A Hidden Radio Option With Keyboard Navigation In Firefox And Ie?

UPDATE The following problem occurs even after trying out the suggestions here. The latest code snippet demonstrates all 3 approaches of hiding a Radio Button and breaking ↓ / �

Solution 1:

I only solved it with a manual workround, where I intercept the Up/Down keys and make it jump over the hidden element. Works in all 3 browsers (FF/IE/Chrome) and wraps around if necessary. Surprising that a hack is required and no other info is available anywhere.

$('#container').on('keydown', 'input', function(e) {

    var groupname = $(this).attr('name');
    var groupindex = $('[name="' + groupname +  '"]').index($(this));
    var groupsize = $('[name="' + groupname + '"]').length;     

    // For Down Arrow, if subsequent input in group is hidden, focus the one after it (wrap around if necessary)
    if (e.keyCode == 40 && 
        $('[name="' + groupname + '"]').eq(groupindex + 1).length && 
        $('[name="' + groupname + '"]').eq(groupindex + 1).is(':hidden')) 
    {
        e.preventDefault();
        $('[name="' + groupname + '"]').eq((groupindex + 2) % groupsize).focus();
        $('[name="' + groupname + '"]').eq((groupindex + 2) % groupsize).prop('checked', true);
        $('[name="' + groupname + '"]').trigger('change'); // Trigger Change Event manually for any dependencies
        return false;
    }
    // For Up Arrow, if preceding input in group is hidden, focus andselect the one before it (wrap around if necessary)
    elseif (e.keyCode == 38 && 
            $('[name="' + groupname + '"]').eq(groupindex - 1).length && 
            $('[name="' + groupname + '"]').eq(groupindex - 1).is(':hidden')) 
    {
        e.preventDefault();
        $('[name="' + groupname + '"]').eq((groupindex - 2) % groupsize).focus();
        $('[name="' + groupname + '"]').eq((groupindex - 2) % groupsize).prop('checked', true);
        $('[name="' + groupname + '"]').trigger('change'); // Trigger Change Event manually for any dependencies
        return false;
    }

    return true;
});

Full Demo Snippet

	$('#container').on('keydown', 'input', function(e) {
		
		var groupname = $(this).attr('name');
		var groupindex = $('[name="' + groupname +  '"]').index($(this));
		var groupsize = $('[name="' + groupname + '"]').length;		
		
		// For Down Arrow, if subsequent input in group is hidden, focus the one after it (wrap around if necessary)if (e.keyCode == 40 && 
			$('[name="' + groupname + '"]').eq(groupindex + 1).length && 
			$('[name="' + groupname + '"]').eq(groupindex + 1).is(':hidden')) 
		{
			e.preventDefault();
			$('[name="' + groupname + '"]').eq((groupindex + 2) % groupsize).focus();
			$('[name="' + groupname + '"]').eq((groupindex + 2) % groupsize).prop('checked', true);
			$('[name="' + groupname + '"]').trigger('change'); // Trigger Change Event manually for any dependenciesreturnfalse;
		}
		// For Up Arrow, if preceding input in group is hidden, focus and select the one before it (wrap around if necessary)elseif (e.keyCode == 38 && 
				$('[name="' + groupname + '"]').eq(groupindex - 1).length && 
				$('[name="' + groupname + '"]').eq(groupindex - 1).is(':hidden')) 
		{
			e.preventDefault();
			$('[name="' + groupname + '"]').eq((groupindex - 2) % groupsize).focus();
			$('[name="' + groupname + '"]').eq((groupindex - 2) % groupsize).prop('checked', true);
			$('[name="' + groupname + '"]').trigger('change'); // Trigger Change Event manually for any dependenciesreturnfalse;
		}
		
		returntrue;
	});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script><divid="container"><div><inputtype="radio"id="opt1"value="1"name="group"><labelfor="opt1">Option 1</label></div><div><inputtype="radio"id="opt2"value="2"name="group"><labelfor="opt2">Option 2</label></div><div><inputtype="radio"id="opt3"value="3"name="group"><labelfor="opt3">Option 3</label></div><divstyle="display:none;"><inputtype="radio"id="optSecret"value="secret"name="group"><labelfor="optSecret">Option Secret</label></div><div><inputtype="radio"id="opt5"value="5"name="group"><labelfor="opt5">Option 5</label></div></div>

Solution 2:

To whomever comes across this and needs a solution for skipping more than one hidden radio at a time like i did i modified the accepted answer to accommodate skipping more than one hidden radio.

$("#container").on('keydown', 'input', function(e) {

        var groupname = $(this).attr('name');
        var group = $('[name="' + groupname +  '"]:visible');
        var groupindex = group.index($(this));
        var groupsize = group.length;

        // For Down Arrow, if subsequent input in group is hidden, focus the one after it (wrap around if necessary)
        if (e.keyCode == 40) {
            e.preventDefault();
            group.eq((groupindex + 1) % groupsize).focus();
            group.eq((groupindex + 1) % groupsize).prop('checked', true);
            group.eq((groupindex + 1) % groupsize).trigger('change'); // Trigger Change Event manually for any dependencies
            return false;
        }
        // For Up Arrow, if preceding input in group is hidden, focus andselect the one before it (wrap around if necessary)
        elseif (e.keyCode == 38 && group.eq(groupindex - 1).length) {
            e.preventDefault();
            group.eq((groupindex - 1) % groupsize).focus();
            group.eq((groupindex - 1) % groupsize).prop('checked', true);
            group.eq((groupindex - 1) % groupsize).trigger('change'); // Trigger Change Event manually for any dependencies
            return false;
        }
        return true;
    });

Solution 3:

I ran into this issue recently and I figured out if you set the disabled attribute to true on the radio buttons you want to hide, along with setting display: none, you will be able to use the up/down arrow keys to cycle through the displayed radio buttons in the same radio button group without an issue. This works with Firefox, Chrome, and IE 11 (not sure if it will work with older versions of IE).

Post a Comment for "How To Skip A Hidden Radio Option With Keyboard Navigation In Firefox And Ie?"