Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to select by attribute

I have a HTML like this,

<a id="a_1" href="#" disabled_onclick="true">Link1</a>
<a id="a_2" href="#">Link2</a>
<a id="a_3" href="#" disabled_onclick="true">Link3</a>
<input id="b_1" type="submit" disabled_onclick="true">Button1</input>
<input id="b_2" type="submit">Button2</input>
<input id="b_3" type="submit">Button3</input>

Now I need write a jQuery which returns me all the attributes in my html having a disabled_onclick property set to true. In this case, I should get 3 elements, two link tags and one input tag.

like image 221
bragboy Avatar asked Feb 09 '12 11:02

bragboy


2 Answers

Here’s how to select all those elements:

$('[disabled_onclick="true"]');

Since true is a valid unquoted attribute value in CSS, you could even omit the quotes:

$('[disabled_onclick=true]');

If you care about valid HTML you should consider using a custom data-* attribute instead though.

<input id="b_1" type="submit" disabled_onclick="true">
<!-- …becomes… -->
<input id="b_1" type="submit" data-disabled-onclick="true">

That way it’s valid HTML, and you’ll still be able to select it as follows:

$('[data-disabled-onclick="true"]');
like image 76
Mathias Bynens Avatar answered Oct 02 '22 15:10

Mathias Bynens


try this

$('[disabled_onclick="true"]')
like image 22
dku.rajkumar Avatar answered Oct 02 '22 16:10

dku.rajkumar