JavaScript gets the sample code for the specified object on the current page

  • 2020-03-30 02:11:47
  • OfStack

How JavaScript gets the specified object on the current page.

Here's how:
document.getElementById(ID) //Gets an object with the specified ID value
document.getElementsByName(Name) //Gets an array of objects with the specified Name value
document.all[] //Smart stuff but not a WEB standard
document.getElementsByTagName //Gets an array of objects for the specified tag value

Here's an example, just run it without the comments.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<TITLE> New Document </TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT=""> 
<META NAME="Keywords" CONTENT=""> 
<META NAME="Description" CONTENT=""> 

<form method="post" name="mainFrm" action=""> 
<input type="hidden" name="text" id="text" style="width:100%" value=" practice "> 
<input type="hidden" name="organizationId" style="width:100%" value=" validation DOCUMENT.ALL"> 
<table width="100%" border="1"> 
<tr height="22"> 
<td width="15%" align="right"> Inventory organization: </td> 
<td width="20%"><input type="text" name="organizationId" id="organizationId" style="width:100%" value=" Inventory organization "></td> 
<td width="15%" align="right"> Sublibrary code: </td> 
<td width="20%"> 
<select name="subinventoryCode" style="width:100%" id="subinventoryCode"> 
<option value="QTWL">QTWL</option> 
<option value="BTSPT">BTSPT</option> 
<option value="BTS">BTS</option> 
<tr height="22"> 
<td colspan="2" align="center"><input type="button" value=" Gets the specified object " onclick="do_check()"></td> 
function do_check(){ 
//GetElementById: you get the element by ID, so you can only access the element with the ID set.
//The return value of the method is guaranteed to be the object you need, because the ID value of an object on the entire page is unique.
// var organizationId = document.getElementById("organizationId"); 
// alert(organizationId.value); 

//GetElementsByName: just gets the element by NAME.
//The return value of the method is an array, and it will return as an array even if there is only one object with a given name attribute in the entire page.
//It's just that the length of the array is one.
// var organizationId = document.getElementsByName("organizationId"); 
// alert(organizationId[0].value); 
// alert(organizationId.length); 

//GetElementsByTagName: gets the element by TAGNAME, of course, the same tag in a DOCUMENT,
//So this method and getElementsByName are also getting an array, just the difference in how you get 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 of all the tags in the document, including all the elements in the document object.
//Getting a specified element is usually done by name, but it is smarter than getElementsByName in that if one of the qualified objects is returned, multiple are returned as arrays
// var organizationId = document.all["organizationId"]; 
// alert(organizationId[0].value) 
// document.all["organizationId"] 
// document.all.item("organizationId") 

Related articles: