hyperrjas
hyperrjas

Reputation: 10744

refactor jquery code to one line of code

I have this jquery code:

$('#csv_button').attr('disabled', 'disabled').addClass("disabled");
$('#excel_button').attr('disabled', 'disabled').addClass("disabled");
$('#delete_button').attr('disabled', 'disabled').addClass("disabled");

Is it possible simplify this code to one line of code?

Upvotes: 0

Views: 79

Answers (3)

Gurpreet Singh
Gurpreet Singh

Reputation: 21233

Or you can do:

  $("[id$='button']").prop('disabled', true).addClass("disabled");

This will return all elements with id ending with 'button'.

More information on jquery selectors can be found here.

Upvotes: 0

jrummell
jrummell

Reputation: 43077

You can separate multiple selectors with commas:

$('#csv_button,#excel_button,#delete_button').prop('disabled', true).addClass("disabled");

Or you could apply the same class to all 3 elements and use a class selector:

$('.disabled-button').prop('disabled', true).addClass("disabled");

Upvotes: 6

KingKongFrog
KingKongFrog

Reputation: 14419

$('#csv_button,#excel_button,#delete_button').attr('disabled','disabled').addClass("disabled");

Upvotes: 0

Related Questions