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>
1
  • 2
    You cannot use CSS to modify DOM Properties. Commented Sep 15, 2014 at 11:06

3 Answers 3

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;          
}
Sign up to request clarification or add additional context in comments.

1 Comment

@chaya you can use pointer-events: none, but it lacks support
0

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

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

}

Or you can use solution suggested here

Comments

0

you can do it in jquery only

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.