0

I want to create a CSS class to make input field is readonly.

Here is my try:

 <style>
 .enableInput input[readonly] {
  color: #CB000F;
 }
</style>
<script>
function changeCss()
{
$("#desg").addClass("enableInput input");
}
</script>
</head>
FirstName:<input type="text" name='fname' id='fname' onBlur="changeCss();">
Designation:<input type="text" name='desg' id='desg' value='Software Engr'>
</html>
chaya
  • 423
  • 3
  • 11
  • 19

3 Answers3

6

You don't need to do anything with CSS. there is already a readonly attribute present in html input:

<input type="text" name="country" value="Norway" readonly>

See more here

There is no such CSS property which would simply make an input readonly, but in this question, @James Donnelly gave a workaround to achieve that:

.enableInput{
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;          
}
Community
  • 1
  • 1
Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43
  • @chaya you can use `pointer-events: none`, but it [lacks support](http://caniuse.com/pointer-events) – Vucko Sep 15 '14 at 11:13
0

Yes, you can use the :disabled pseudo class, e.g.

input.myClass:disabled {
/* style declaration here */

}

Or you can use solution suggested here

Community
  • 1
  • 1
Scription
  • 646
  • 3
  • 12
  • 21
0

you can do it in jquery only

function changeCss()
{ 
$('#desg').prop('disabled', true);
}