1

I am showing or hiding divs elements on a single page using jQuery depending on what is clicked:

HTML:

<div id='aboutPage' class='visible'>
//content
</div>

<div id='contactPage' class='invis'>
//content
</div>

CSS:

.visible { display: block; }
.invis { display: none; }

JS:

$('#element1').on('click', {clickedTab: 'aboutTab'}, changeTab);
$('#element2').on('click', {clickedTab: 'contactTab'}, changeTab);

var currentTab = 'about';

function changeTab(event)
{
//if-else ladder to make invis the prior current tab
if (currentTab == 'about')
    if (event.data.clickedTab != 'aboutTab')
        $('#aboutPage').removeClass('visible').addClass('invis');
    else return;
else if (currentTab == 'contact')
    if (event.data.clickedTab != 'contactTab')
        $('#contactPage').removeClass('visible').addClass('invis');
    else return;

//if-else ladder to change the current tab
if (event.data.clickedTab == 'aboutTab') {
    currentTab = 'about';
    $('#aboutPage').removeClass('invis').addClass('visible');
}
else if (event.data.clickedTab == 'contactTab') {
    currentTab = 'contact';
    $('#contactPage').removeClass('invis').addClass('visible');
}

My question is, is it appropriate for element1 and element2 to be <a> elements, even though they don't technically link to another page? I would like to do this so that stylistically the elements appear to be links to the user. Or, should I make them <span> elements and then just style them to look like links using CSS? Is there any difference? Would the <a> option be considered bad coding style?

2 Answers2

2

My question is, is it appropriate for element1 and element2 to be <a> elements, even though they don't technically link to another page?

There is no requirement that an anchor element link to another page. An anchor element, when used with the href attribute, creates a link. That link can go anywhere: within the same page, an e-mail address, another page, etc.

https://www.w3.org/TR/html5/text-level-semantics.html#the-a-element

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
1

Screen Readers will parse pages and provide content audibly for the visually impaired. One of the ways the visually impaired scan a page is to get a "list of links" on the page. If you are navigating between experiences (showing and hiding content), it makes sense to use a BUTTON or an A element. Using a SPAN is ambiguous, and does not make for an accessible web.

If another user has a disability which prohibits the usage of a mouse (or equivalent), and is limited to keyboard navigation of a page, a SPAN will never achieve focus when tabbing through a page, whereas an A element or a BUTTON will.

<a href="javascript:void(0);">your link</a>

Specifying the aforementioned HREF attribute will prevent your page from jumping around, and allow your javascript to handle the transitions as you intend.

Rajan Patel
  • 83
  • 1
  • 4