Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable html input element by applying custom css class

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);?

like image 917
Ashish Panery Avatar asked Aug 08 '12 11:08

Ashish Panery


People also ask

How do I make input field Disabled in CSS?

To disable form fields, use the CSS pointer-events property set to “none”.

How do you disable an element in CSS?

To disable a HTML anchor element with CSS, we can apply the pointer-events: none style. pointer-events: none will disable all click events on the anchor element. This is a great option when you only have access to class or style attributes. It can even be used to disable all the HTML links on a page.

How do you turn off input in HTML?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable. Tip: Disabled <input> elements in a form will not be submitted!


3 Answers

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);
like image 154
nnnnnn Avatar answered Sep 19 '22 00:09

nnnnnn


You can use following css to practically disable the input:

pointer-events: none;
like image 25
Emircan Avatar answered Sep 19 '22 00:09

Emircan


Using normal CSS

.disabled{
    opacity: 0.6;
    cursor: not-allowed;
    pointer-events: none;
}
like image 29
Yallaling Goudar Avatar answered Sep 22 '22 00:09

Yallaling Goudar