❮ Input Text Object Show
ExampleChange the value of a text field: document.getElementById("myText").value = "Johnny Bravo"; Try it Yourself » Definition and UsageThe value property sets or returns the value of the value attribute of a text field. The value property contains the default value OR the value a user types in (or a value set by a script). Browser Support
SyntaxReturn the value property: Set the value property: Property Values
Technical Details
More ExamplesExampleGet the value of a text field: var x = document.getElementById("myText").value; Try it Yourself » ExampleForm validation: var at = document.getElementById("email").value.indexOf("@"); if (fname.length > 10) { if (isNaN(age) || age < 1 || age > 100) { if (at == -1) { if (submitOK == "false") { Try it Yourself » ExampleDropdown list in a form: var mylist = document.getElementById("myList"); Try it Yourself » ExampleAnother dropdown list: var no = document.getElementById("no"); Try it Yourself » ExampleAn example that shows the difference between the defaultValue and value property: var x = document.getElementById("myText"); Try it Yourself » Related PagesHTML reference: HTML <input> value attribute ❮ Input Text Object There are various methods to get an input textbox value directly (without wrapping the input element inside a form element): Method 1
For example
Note: Method 2,3,4 and 6 returns a collection of elements, so use [whole_number] to get the desired occurrence. For the first element, use Method 2Use
For example
Method 3Use For example
Method 4
For example
Method 5Use the powerful For example
Method 6
For example
Support
Useful links
How can I get the value entered in the input field?Input Text value Property. Change the value of a text field: getElementById("myText"). ... . Get the value of a text field: getElementById("myText"). ... . Dropdown list in a form: var mylist = document. ... . Another dropdown list: var no = document. ... . An example that shows the difference between the defaultValue and value property:. What is the value of an input field?The value attribute specifies the value of an <input> element. The value attribute is used differently for different input types: For "button", "reset", and "submit" - it defines the text on the button. For "text", "password", and "hidden" - it defines the initial (default) value of the input field.
Can we get element by value in JavaScript?Introduction to JavaScript getElementsByName() method
The getElementsByName() accepts a name which is the value of the name attribute of elements and returns a live NodeList of elements. The return collection of elements is live.
How do you take input in JavaScript?In JavaScript, we use the prompt() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “ok,” the input value is returned.
|