How to get the specified object on the current page in JavaScript.
The method is as follows:
The code copy is as follows:
document.getElementById(ID) // Obtain the object with the specified ID value
document.getElementsByName(Name) //Get an array of objects with specified Name value
document.all[] //Smart things are not WEB standard
document.getElementsByTagName //Get an array of objects with specified tag value
Here is an example, just remove the comment and run it directly to see the effect.
The code copy is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<form method="post" name="mainFrm" action="">
<input type="hidden" name="text" id="text" value="just practice">
<input type="hidden" name="organizationId" value="Verify DOCUMENT.ALL">
<table>
<tr>
<td align="right">Inventory organization:</td>
<td><input type="text" name="organizationId" id="organizationId" value="Inventory Organization"></td>
</tr>
<tr>
<td align="right">Sub-store encoding:</td>
<td>
<select name="subinventoryCode" id="subinventoryCode">
<option value="QTWL">QTWL</option>
<option value="BTSPT">BTSPT</option>
<option value="BTS">BTS</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="get the specified object" onclick="do_check()"></td>
</tr>
</table>
</form>
</BODY>
</HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
function do_check(){
// getElementById: It is to obtain elements through ID, so you can only access elements with ID set.
// The return value of the method can be ensured to be the object you need, because the ID value of an object in the entire page is unique.
// var organizationId = document.getElementById("organizationId");
// alert(organizationId.value);
// getElementsByName: It is to obtain elements through NAME.
// The return value of the method is an array. Even if there is only one object with the name attribute as a given value in the entire page, it will be returned as an array.
// It's just that the array length is one at this time.
// var organizationId = document.getElementsByName("organizationId");
// alert(organizationId[0].value);
// alert(organizationId.length);
// getElementsByTagName: Get elements through TAGNAME (tag name). Of course, there will be the same tag in a DOCUMENT.
// So this method and getElementsByName are basically aware of it, and it also gets an array, but it is just the difference in the way of getting the object.
// var inputs = document.getElementsByTagName("input");
// alert(inputs.length);
// alert(inputs[0].value);
// alert(inputs[1].value);
// alert(inputs[2].value);
// document.all[] is an array variable composed of all labels in the document, including all elements in the document object.
// Generally, the specified element is obtained by name, but it is smarter than getElementsByName. If there is one object that meets the criteria, it will return this object, and multiple will return it in an array.
// var organizationId = document.all["organizationId"];
// alert(organizationId[0].value)
// document.all["organizationId"]
// document.all.item("organizationId")
}
//-->
</SCRIPT>