7

I want to disable my all input element of a div by applying my custom css class. but i could not find any css attribute which can disable an input element. currently what am i doing

  $('#div_sercvice_detail :input').attr('disabled', true);
 $('#retention_interval_div :input').addClass("disabled"); 

which can disable all input element of div with css attr but i want to apply my custom class for disable all input with some extra css attributes

 $('#retention_interval_div :input').addClass("disabled");

class

.disabled{
color : darkGray;
font-style: italic;
/*property for disable input element like*/
/*disabled:true; */
}    

any suggestion for doing this with jquery without using .attr('disabled', true);?

Ashish Panery
  • 1,196
  • 3
  • 13
  • 24
  • 4
    Well, `.prop('disabled',true)` is how you _should_ do it with the current version of jQuery - which meets your request for a way to avoid the `.attr()` method. – nnnnnn Aug 08 '12 at 11:26
  • +1 for the .prop('disabled', true) – DerWaldschrat Aug 08 '12 at 11:27
  • 2
    There is no CSS property to specify `disabled`. jQuery's `.prop()` is the better method. Though I'm assuming you want to avoid that as well as `attr()`. May I ask why? – David Thomas Aug 08 '12 at 11:29
  • 4
    You can't disable an element with just CSS, but you CAN accept an answer to each of the 8 previous questions you've posted here. – chrisfrancis27 Aug 08 '12 at 11:29
  • possible duplicate of [How do I disable form fields using CSS?](http://stackoverflow.com/questions/5963099/how-do-i-disable-form-fields-using-css) – Ciro Santilli OurBigBook.com Jul 05 '14 at 12:06

6 Answers6

10

There is no way to disable an element with just CSS, but you can create a style that will be applied to disabled elements:

<style>
#retention_interval_div​​ input[type="text"]:disabled { 
    color : darkGray;
    font-style: italic;
}​
</style>

Then in your code you just have to say:

 $('#retention_interval_div :input').prop("disabled", true);

Demo: http://jsfiddle.net/nnnnnn/DhgMq/

(Of course, the :disabled CSS selector isn't supported in old browsers.)

Note that if you're using jQuery version >= 1.6 you should use .prop() instead of .attr() to change the disabled state.

The code you showed is not disabling the same elements that it applies the class to - the selectors are different. If that's just a typo then you can simplify it to one line:

$('#retention_interval_div :input').addClass("disabled").attr('disabled', true);
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
7

You can use following css to practically disable the input:

pointer-events: none;
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Emircan
  • 71
  • 1
  • 2
4

Using normal CSS

.disabled{
    opacity: 0.6;
    cursor: not-allowed;
    pointer-events: none;
}
2

No. CSS can't disable an input element. CSS is for styling only, it can't do anything else. Also, input will only work with input, don't forget select, textarea, password

Gareth Parker
  • 5,012
  • 2
  • 18
  • 42
  • 1
    The [`:input` selector](http://api.jquery.com/input-selector/) basically selects _all_ form controls, not just ``. – nnnnnn Aug 08 '12 at 11:45
1

You need to do 2 things, so why not wrap them in a single function? You could even create a little plugin to do this:

(function ($) {
    $.fn.disableInput = function () {
        return this.each(function(){
            $(this).prop('disabled');
            $(this).addClass('disabled', true);            
        });

    }
})(jQuery);

Then you can call it like this:

$('#myInput').disableInput();

...and even chain it with other stuff, like this:

$('#myInput').disableInput().addClass('otherClass');​
Faust
  • 15,130
  • 9
  • 54
  • 111
  • Note that you don't need the `.each()`, you can say `this.prop().addClass()`. – nnnnnn Aug 08 '12 at 11:46
  • Don't you mean `$(this).prop('disabled', true);` And anyway, wouldn't you want disableInput() to have a true/false parameter? – Bob Stein Sep 19 '16 at 00:03
0

You can't disable input elements using css properties. But you can improve your current coding like following

 $('#div_sercvice_detail :input').prop('disabled',true).addClass("disabled");
Nick
  • 1,799
  • 3
  • 23
  • 32