javascriptjqueryhtml-input

How to set the value of an input field element?


This is an example what i need to do.

$(document).ready(function() {
  var inputvalue = "field 1";
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div><input value="<!-- inputvalue here -->"></div>


Solution

  • $(document).ready(function() {
      var inputvalue="field 1";
      $('#id1').val(inputvalue);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    
    <html>
    
    <body>
      <div><input id="id1" value=""></div>
    </body>
    
    </html>