How to display value in textbox using JavaScript
To display a value in a textbox using JavaScript, you can use the value property of the textbox element. The example is given below.
In the above example, we use getElementById() method to select the textbox element with the ID "my-textbox". Then, we set its value property to "How are you?" in order to display that text in the textbox.
How to display variable value in textbox in JavaScript#
Alternatively, if you want to display the value of a variable in the textbox, you can use use the following example.
In the above example, we assign the value "8" to the variable myVariable. Then, we display the value of that variable in the textbox, along with some additional text.
Get the Value from Prompt in the input text#
To display a value in a textbox using the prompt() method in JavaScript, you can combine the prompt() method with the value property of the textbox element.
In the above example, we create a textbox element with an ID of "my-textbox" and a button element with an "onclick" attribute that calls the myFunction() function. When the user clicks the button, the myFunction() function is called, which creates a variable called textbox which contains a reference to the textbox element.
Then, the function uses the prompt() method to display a dialog box and ask the user to enter a value. The entered value is stored in a variable called "myValue". Finally, the function sets the value property of the textbox element to the entered value.
<input type="text" id="my-textbox" placeholder="My Textbox">
<button onclick="myFunction()">Submit</button>
<script>
function myFunction(){
let textBox = document.getElementById("my-textbox");
let myValue = prompt("Enter a value: ");
textBox.value = myValue;
}
</script>