0

I have a button on my page which executes a JavaScript function on click. Now I have a variable in my function whose value should be the one which user enters in input text box. Here is my code.

    <input type="text"> </input>
    <button id ="buttton" class="button" onclick="myfunction()">Create Envelope</button>

    <script>
    function myfunction() {
    var sub =""; // I am thinking of writing jquery 
}
   </script>

Now by the time user clicks the button my variable sub should hold the value which user enters in the input field.

Srujan
  • 93
  • 1
  • 10

1 Answers1

3

Use .value property of InputElement(JavaScript) or $(ELEMENT).val() method of jQuery

To select the element, give unique ID to element. There are other ways to select the element as well which are class-selector or attribute-selector and many more!

function myfunction() {
  var sub = $('#input').val();
  console.log(sub);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" id='input'>
<button id="buttton" class="button" onclick="myfunction()">Create Envelope</button>
Rayon
  • 36,219
  • 4
  • 49
  • 76