3

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

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I should have mentioned i have two buttons- Day and Night with matching data attributes data-file="day" and data-file="night. Can I make an object of both buttons i.e var $button = $('button') then select the day button via its data attribute?
Perfecto! Many thanks. Clearly I need to learn more methods. Many thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.