New dev here trying to get comfortable with JavaScript/jQuery.
I'm currently developing a page with tabs that shuffles content on my page based on which tab (button) is selected. The tabs are based on this code: https://codepen.io/broskibro/pen/VwKRmKQ
<input type="radio" id="tab1" name="tab-control" checked>
<input type="radio" id="tab2" name="tab-control">
<ul>
<li id="list1" title="Job Seeker"><label tabindex="0" class="btn lg" for="tab1" role="button">
<br><span>Job Seeker</span></label></li>
<li id="list2" title="Employer"><label tabindex="0" class="btn lg" for="tab2" role="button">
<br><span>Employer</span></label></li>
</ul>
I've gotten all the functionality that I need from it, but the outstanding issue is that I need the feature to be accessible. It's easy enough to add tab-index=0
to the buttons so that they can be reached via tabbing, but the problem is that the input
is what needs to be tab-indexed to and "Enter-ed" to actually activate the functionality. I want to make it so that I can use JS so that when I'm focused on the buttons and I press enter, the respective is activated. I'm assuming it will be something using the keydown
feature. I'm repurposing some code I got to work for another project I was working on, but I imagine it would look something like:
var input = document.getElementById("list1")
input.addEventListener("keydown", function(event) {
if (event.key == 'Enter') {
// Trigger the click for the input
document.getElementById("tab1").click();
}
});
It looks like it should be a relatively easy solution but I'm getting a variety of different errors. Any guidance is appreciated!