Get textbox value in jquery but empty

I use this code for getting value from text box. But I can't able to get value.

enter image description here

$(function() {
  var get = $(".name").val();
  $('#test').click(function() {
    alert('Textbox:' + get);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="name" class="name">
<input type="submit" value="click" name="test" id="test">

Upvotes: 0

Views: 59

Answers (2)

Asim
Asim

Reputation: 976

Get your value when you click on #test.

$(function() {
  $('#test').click(function() {
    var get = $('.name').val();
    alert('Textbox:' + get);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="name" class="name">
<input type="submit" value="click" name="test" id="test">

Upvotes: 2

valek
valek

Reputation: 1376

The problem was that you were setting the get value at the beginning only, when the input box was empty. You have to update the value when you click on the submit button.

$(function() {
  
  $('#test').click(function() {
    let get = $(".name").val();
    alert('Textbox:' + get);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="name" class="name">
<input type="submit" value="click" name="test" id="test">

Upvotes: 0

Related Questions