Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does role="button" mean?

I found that in Google+ Project's page that buttons are all made from divs like:

<div role="button"></div>   

I'd like to know, is this just for semantic purposes, or does it influence the style or event handling of the <div>?

I tried to simulate a button "click" with the jQuery click event, but it didn't work.

like image 515
wong2 Avatar asked Jul 03 '11 10:07

wong2


People also ask

Is role button the same as button?

The button role is used to make an element appear as a button control to a screen reader and can be applied to otherwise non-interactive elements like <div> .

Do you need role on a button?

You do not need to use role=button in general, as button elements have strong native semantics.

What is role in a tag?

The role attribute describes the role of an element in programs that can make use of it, such as screen readers or magnifiers. Usage Example: <a href="#" role="button">Button Link</a> Screen Readers will read this element as “button” instead of “link”.

What is the use of role in accessibility?

ARIA roles provide semantic meaning to content, allowing screen readers and other tools to present and support interaction with object in a way that is consistent with user expectations of that type of object.


1 Answers

It tells accessibility (and other) software what the purpose of the div is. More here in the draft role attribute specification.

Yes, it's just semantic. Sending a click event to the button should work.


An earlier version of this answer (back in 2011) said:

...but jQuery's click function doesn't do that; it triggers only the event handlers that have been hooked up to the element with jQuery, not handlers hooked up in other ways.

...and provided the sample code and output below.

I cannot replicate the output now (two years later). Even if I go back to earlier versions of jQuery, they all trigger jQuery handlers, DOM0 handlers, and DOM2 handlers. The order isn't necessarily the same between a real click and jQuery's click function. I've tried jQuery versions 1.4, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.5, 1.5.1, 1.5.2, 1.6, and more recent releases such as 1.7.1, 1.8.3, 1.9.1, and 1.11.3 (the current 1.x release as of this writing). I can only conclude that it was a browser thing, and I don't know what browser I was using. (Right now I'm using Chrome 26 and Firefox 20 to test.)

Here's a test which shows that indeed, jQuery handlers, DOM0 handlers, and DOM2 handlers are all (as of this writing!) triggered by jQuery's click:

jQuery(function($) {    var div;      $("<p>").text("jQuery v" + $.fn.jquery).appendTo(document.body);      // Hook up a handler *not* using jQuery, in both the DOM0 and DOM2 ways    div = document.getElementById("theDiv");    div.onclick = dom0Handler;    if (div.addEventListener) {      div.addEventListener('click', dom2Handler, false);    } else if (div.attachEvent) {      div.attachEvent('onclick', dom2Handler);    }      // Hook up a handler using jQuery    $("#theDiv").click(jqueryHandler);      // Trigger the click when our button is clicked    $("#theButton").click(function() {      display("Triggering <code>click</code>:");      $("#theDiv").click();    });      function dom0Handler() {      display("DOM0 handler triggered");    }      function dom2Handler() {      display("DOM2 handler triggered");    }      function jqueryHandler() {      display("jQuery handler triggered");    }      function display(msg) {      $("<p>").html(msg).appendTo(document.body);    }    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  <div id="theDiv">Try clicking this div directly, and using the button to send a click via jQuery's <code>click</code> function.</div>  <input type='button' id='theButton' value='Click Me'>
like image 68
T.J. Crowder Avatar answered Oct 02 '22 20:10

T.J. Crowder