Please make this code more accessible, providing complete HTML, Javascript, and CSS.…
- User
Please make this code more accessible, providing complete HTML, Javascript, and CSS. Ensure that the example is keyboard operable and accessible for screen readers.
HTML:
Javascript: (function() { var list = ('.js-dropdown-list'); var link = ('.js-link'); link.click(function(e) { e.preventDefault(); list.slideToggle(200); }); list.find('li').click(function() { var text = (this).html(); var icon = ''; link.html(text+icon); list.slideToggle(200); if (text === '* Reset') { link.html('Select one option'+icon); } }); });
- Assistant
CSS: .dropdown { position: relative; display: inline-block; width: 150px; font-size: 16px; font-weight: bold; text-align: center; color: #333; }
.dropdown a { display: block; padding: 10px; color: #333; text-decoration: none; }
.dropdown a:hover { background-color: #eee; }
.dropdown ul { position: absolute; top: 100%; left: 0; right: 0; display: none; margin: 0; padding: 0; list-style: none; background-color: #fff; border: solid 1px #ccc; }
.dropdown li { display: block; padding: 10px; cursor: pointer; color: #333; }
.dropdown li:hover { background-color: #eee; }
.dropdown li:first-child { border-bottom: solid 1px #ccc; }
.dropdown li:last-child { font-style: italic; color: #999; }
.dropdown i { margin-left: 5px; }
Javascript (with added keyboard functionality): (function() { var list = ('.js-dropdown-list'); var link = ('.js-link'); var isOpen = false; link.click(function(e) { e.preventDefault(); isOpen = !isOpen; if (isOpen) { list.slideDown(200); list.find('li:first-child a').focus(); } else { list.slideUp(200); link.focus(); } }); list.find('li a').click(function(e) { e.preventDefault(); var text = (this).html(); var icon = ''; link.html(text+icon); list.slideUp(200); isOpen = false; if (text === '* Reset') { link.html('Select one option'+icon); } link.focus(); }); list.keydown(function(e) { var selected = $(this).find('li a:focus'); if (e.keyCode === 27) { // Esc key isOpen = false; list.slideUp(200); link.focus(); } else if (e.keyCode === 38) { // Up arrow e.preventDefault(); if (selected.parent().is(':first-child')) { selected.blur(); link.focus(); } else { selected.parent().prev().find('a').focus(); } } else if (e.keyCode === 40) { // Down arrow e.preventDefault(); if (selected.parent().is(':last-child')) { selected.blur(); link.focus(); } else { selected.parent().next().find('a').focus(); } } }); link.keydown(function(e) { if (e.keyCode === 13 || e.keyCode === 32) { // Enter or Space key e.preventDefault(); isOpen = true; list.slideDown(200); list.find('li:first-child a').focus(); } else if (e.keyCode === 38 || e.keyCode === 40) { // Up or Down arrow e.preventDefault(); isOpen = true; list.slideDown(200); list.find('li:first-child a').focus(); } }); });
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.