Advertisements
How to Get the Value in an Input Text Box using jQuery
Topic: JavaScript / jQueryPrev|Next
Answer: Use the jQuery val()
Method
You can simply use the jQuery val()
method to get the value in an input text box.
Try out the following example by entering something in the text input box and then click the "Show Value" button, it will display the result in an alert dialog box.
Example
Try this code »<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Get the Value of an Input Text Box</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
// Get value on button click and show alert
$("#myBtn").click(function(){
var str = $("#myInput").val();
alert(str);
});
});
</script>
</head>
<body>
<input type="text" id="myInput">
<button type="button" id="myBtn">Show Value</button>
</body>
</html>
Related FAQ
Here are some more FAQ related to this topic:
- How to check if an input field is empty using jQuery
- How to detect change in a text input box in jQuery
- How to detect the enter key press in a text input field using jQuery
- How to get selected file name from input type file using jQuery
Advertisements