Can anyone show me how to get to a text edit box in a html page and set its value 开发者_运维百科using Java script please?
Given a text box
<input type="text" id="MyInput">
You Javascript would look like:
document.getElementById("MyInput").value = "some value";
There will be other ways of doing this if you are using a JavaScript framework, for example in jquery, the following will suffice:
$("#MyInput").val("some value");
<input type="text" id="ExampleTextBox" />
If you know the id of it:
document.getElementById(TEXTBOX_ID).value = YOUR_VALUE;
or in a function:
function chanageTextBoxValue(newValue)
{
document.getElementById('ExampleTextBox').value = newValue;
}
You can access it through your form:
document.myForm.firstName.value = "Hello World";
--
<form name="myForm">
<input type="text" name="firstName" />
</form>
精彩评论