1

I have a working javascript version to disable/enable a from button but I can not get it working using jQuery.

My jsfiddle has both versions. The javascript version is commented out.

    //NOT WORKING jQuery
   function controls(id) {
   if (id === "button_start") {
    //$('button_start').prop('disabled','disabled');
    // $('button_stop').removeProp('disabled','disabled');

    // testing
    $('#button_start').on('click',function() {
        $(this).prop('disabled', 'disabled');
    });
    $('#button_stop').on('click', function(){
        $(this).removeProp('disabled', 'disabled');
    });

    //console.log(id);

   } else {
    //$('button_stop').prop('disabled','disabled');
    //$('button_start').removeProp('disabled','disabled');

     // testing
    $('#button_stop').click(function() {
        $(this).prop('disabled', 'disabled');
    });
    $('#button_start').click(function(){
        $(this).removeProp('disabled', 'disabled');
    });
      //console.log(id);
        }
    }

jsFiddle: https://jsfiddle.net/tommy6s/w2u8eskv/

Thank you for your help!

t6s
  • 81
  • 2
  • 14
  • The disabled *property* is a boolean. Set it to true or false, don't set it to a string or try to remove it. – nnnnnn Feb 05 '16 at 00:24
  • This question would be easier to understand if you copied the working JavaScript from the fiddle to the question. – pppery Feb 05 '16 at 00:48

4 Answers4

1

This might not solve your problem, however you are using the removeProp method wrong. According to the jQuery documentation, removeProp takes only one attribute

.removeProp( propertyName )
propertyName
Type: String
The name of the property to remove.

In your example, I would change your lines that look like this

$('#button_start').click(function(){
    $(this).removeProp('disabled', 'disabled');
});

to this

$('#button_start').click(function(){
    $(this).removeProp('disabled');
});

https://api.jquery.com/removeProp/

Also, remember that id elements must start with the # sign. You had that in your OP but not in the fiddle.

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
0

Your selectors are wrong.

$('button_start')

should be

$('#button_start') 
   ^--------------- Missing id selector syntax

Then you need to include the jQuery library for the fiddle to start working.

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
0

Add the style:

#button_start:disabled{background:blue;opacity:0.3;}

and your script:

function controls(id) {

    if (id === "button_start") {
        $('#button_start').attr('disabled','disabled');      
        $('#button_stop').removeProp('disabled');
    } else {
        $('#button_stop').attr('disabled','disabled');
        $('#button_start').removeProp('disabled');
    }
}
dexhering
  • 422
  • 3
  • 13
0

For jQuery versions after 1.6:

$('#button_start').prop('disabled', false); //to enable and pass true to disable

For other options and versions, look at this SO answer Disabling and enabling an HTML input button

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Josh
  • 455
  • 7
  • 12