petko_stankoski
petko_stankoski

Reputation: 10713

What does this jquery code do?

$('[role="button"]').click(function () {
            myMethod();
        });

I know that on click on something it calls myMethod(), but on click on what?

What does role mean?

Is this button?

<input type="button" ... />

What is: [role="button"] ?

Upvotes: 0

Views: 83

Answers (4)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

It is an attribute equals selector. $('[role="button"]') would select all elements that has attribute role set to button.

For ex:
All below three will be selected when you do $('[role="button"]')

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

But this will not

<input type="button">

Upvotes: 8

nickaknudson
nickaknudson

Reputation: 4807

It's the attribute equals selector

Upvotes: 1

Sushanth --
Sushanth --

Reputation: 55740

Selector with attribute role whose value is button

$('[role="button"]')  ; // It is not the button in context

//

<input role="button" ... />  // this is selected
    <input type="button" ... />  // Not this
    <input role="button" ... />  // this is selected

Upvotes: 3

Daniel A. White
Daniel A. White

Reputation: 190925

The selector is selecting on the role attribute that is equal to button.

Upvotes: 1

Related Questions