Reputation: 1
I use this code for getting value from text box. But I can't able to get value.
$(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
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
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