Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select jquery object by data attribute

Tags:

jquery

My html fragment goes something like

<button data-file="day">Day</button>
<button data-file="night">Night</button> 

and I'm trying to do the following in Jquery

var $button = $('button');
$button.data('file' , 'day').attr('disabled', 'disabled');

So make jQuery objects of all buttons then disable the button with data attribute day

like image 617
James Avatar asked May 29 '26 06:05

James


1 Answers

You need to use the attribute selector and need to use .prop() to set the disabled state

var $button = $('button[data-file="day"]');
$button.prop('disabled' , true);

Update

var $button = $('button');
$button.filter('[data-file="day"]').prop('disabled' , true);
like image 72
Arun P Johny Avatar answered May 31 '26 20:05

Arun P Johny