jqueryHow do I get the value of an input field using jQuery?
To get the value of an input field using jQuery, you can use the val()
method. This method is used to get the values of form elements such as input, select, textarea.
Example code
<form>
<input type="text" name="name" id="name" value="John Doe" />
</form>
<script>
var name = $("#name").val();
console.log(name);
</script>
Output example
John Doe
The code above consists of the following parts:
- An HTML form element with an input field
- A jQuery script to get the value of the input field
The HTML form element defines the input field with an id
of name
. The jQuery script uses the $("#name")
selector to target the input field and the val()
method to get its value. The value is then stored in the name
variable and logged to the console.
Helpful links
More of Jquery
- How do I use jQuery ZTree to create a hierarchical tree structure?
- How do I create a jQuery Yes/No dialog?
- How do I add a zoom feature to my website using jQuery?
- How do I use a jQuery zoom plugin?
- How do I use jQuery to zoom in or out on an element?
- How do I get the y-position of an element using jQuery?
- How can I use JQuery with Yii2?
- How do I prevent XSS attacks when using jQuery?
- How to use jQuery to achieve a specific task?
- How do I use jQuery to change the z-index of an element?
See more codes...