Search code examples
javascripthtmlinputvar

Convert input into string/var and use it in an alert


I am using an input and want to covert it into a string/var to print it out in an alert after i click on a button.

I need the input of "Forename:" to add into the button "send" with "onlcick".

The alert is just a debuger. Instead of alert i want to use a function. Using onclick="printPDF(foreName+' '' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');

function startTime() {
  var today = new Date();
  var da = today.getDate();
  var mo = today.getMonth() + 1;
  var ye = today.getFullYear();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  m = checkTime(m);
  s = checkTime(s);
}

function checkTime(i) {
  if (i < 10) {
    i = "0" + i
  }; // add zero in front of numbers < 10
  return i;
}
<p>
  <label>Forename:</label>
  <input class="w3-input w3-white" id="some1" type="text" name="some1" value="asd" maxLength="200">
</p>

<button id="snbtn1" type="submit" class="w3-btn w3-green" onclick="alert(' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');">send</button>


Solution

  • <script>
    function startTime() {
          var today = new Date();
          var da = today.getDate();
          var mo = today.getMonth()+1;
          var ye = today.getFullYear();
          var h = today.getHours();
          var m = today.getMinutes();
          var s = today.getSeconds();
          m = checkTime(m);
          s = checkTime(s);
          var foreName = document.getElementById('some1').value;
    
          alert(foreName+' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');
    }
    
    function checkTime(i) {
      if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
      return i;
    }
    </script>
    <p>
        <label>Forename:</label>
        <input class="w3-input w3-white" id="some1" type="text" name="some1" value="asd" maxLength="200">
    </p>
    
       <button id="snbtn1" type="submit" class="w3-btn w3-green"  onclick="startTime();">send</button>