Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
<!DOCTYPE html>
   <html lang="en">
   <head>
       <meta charset="UTF-8">
       <meta http-equiv="X-UA-Compatible" content="IE=edge">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Document</title>
   </head>
   <body>
       <textarea
          id="textarea"
          class="textarea"
          placeholder="Please write your text here..."
          maxlength=""
        ></textarea>

       <!-- input maxlength value -->
       <form id="myForm" action="">
           Change Max Length Value: <input type="text" name="maxLength"><br>

           <input type="button" onclick="myFunction()" value="Submit form">
       </form>

       <script>
           function myFunction() {
                   var element = document.getElementById("textarea");
                   element.setAttribute("maxlength", "1000");
                 //change the 1000 by input data in submit form.
               }
       </script>
   </body>
   </html>


What I have tried:

The code above is what I've tried.
Posted
Updated 4-Oct-22 17:30pm
v2

To read the value from the input, you need to get a reference to it. For example:
JavaScript
function myFunction() {
	const maxLength = document.getElementsByName("maxLength")[0];
	const element = document.getElementById("textarea");
    element.maxLength = parseInt(maxLength.value, 10);
}
Demo[^]
 
Share this answer
 
Thanks, Richard Deeming. This works
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900