Walter Nuñez Walter Nuñez - 5 months ago 6x
Javascript Question

Jquery method not disabling buttons

I am trying to disable buttons via a JQuery method. It should work by adding attributes and classes to them but it is not working.

I checked that I am sending the correct ID and I am not getting any errors in console but the button is not being disabled.

$('[id^="pro2-consult"]').on("click", function (event, xhr, settings) {

function DisableBTNforConsult(id) {
var cont1='#pro-save'+id;
var cont2='#pro-create'+id;
$(cont1, cont2).prop('disabled', true);
$(cont1, cont2).addClass('btn-disabled');
$(cont1, cont2).attr('disabled', 'disabled');

<button id="pro-save-p-sis-0610" class=" btn btn-sm btn-success" type="button">Save</button>
<button id="pro-create-p-sis-0610" class=" btn btn-sm btn-success" type="button">Create</button>
<button id="pro2-consult-p-sis-0610" class=" btn btn-sm btn-success" type="button">consult</button>

  1. Based on code from the original version of the question, your id is coming up as "p-sis-0610". Then when you append it to "#pro-save" and "#pro-create", you are missing a dash - in between.

    var cont1 = '#pro-save-' + id;
    var cont2 = '#pro-create-' + id;
  2. Construct the jQuery selector as a single variable.

    var selector = cont1 + ', ' + cont2;

Working DEMO:

You could also use jQuery's $(this) to get your id.

var id = $(this).attr('id').substring(12);