0

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">

2 Answers 2

2

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">

Sign up to request clarification or add additional context in comments.

Comments

0

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">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.