How to make a textarea and input type read only using jQuery?


To make a textarea and input type read only, use the attr() method .

For readonly, set the input type text and textarea as read only as shown below:

$('input[type="text"], textarea').each(function(){
   $(this).attr('readonly','readonly');
});

The following is the code to set readonly to textarea and input type:

Example

 Live Demo

<html>
<head>
<title>jQuery Example</title>
   <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script>
      $(document).ready(function() {
         $('input[type="text"], textarea').each(function(){
            $(this).attr('readonly','readonly');
         });
      });

   </script>
</head>
<body>
<form>
<input type="text" value="readonly text field" /><br>
<textarea>readonly textarea</textarea>
</form>
</body>
</html>

Updated on: 20-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements