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

jQueryWeb DevelopmentFront End Technology

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>
raja
Published on 09-Aug-2018 09:28:40
Advertisements