'Javascript: How to store a user generated text in a variable in javascript?
I want to have a text field where user can type in some text and I want to store whatever they typed in a javascript variable? How do I do this?
Solution 1:[1]
var userText = document.getElementById("yourTextInputId").value;
That will store the value of the element with id of "yourTextInputId" in the variable userText. You could put this in a function and call it whenever you need to store the value (for example, when a button is clicked, or when the blur event is fired).
Here's an example binding a blur event to the text input field:
document.getElementById("yourTextInputId").onblur = function() {
var userText = this.value;
}
Solution 2:[2]
var my_var=document.getElementByID["your_text_box"].value;
Solution 3:[3]
this is work with me every time try it
function getname(name) {
name = document.getElementById("txtName").value;
return name;
}
the getname is the function name and you must pass a parameter witch in may case (name) then use
name = document.getElementById("txtName").value;
to store the value in the variable then return this variable now you have to do call the getname(name) function whenever you need.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | James Allardice |
| Solution 2 | Maysam |
| Solution 3 | Ali Hamza |
