JavaScript obtains the sample code of a specified object on the current page.

Source: Internet
Author: User

How does JavaScript obtain the specified object on the current page.

The method is as follows:
Copy codeThe Code is as follows:
Document. getElementById (ID) // get the object with the specified ID value
Document. getElementsByName (Name) // get the object array with the specified Name value
Document. all [] // intelligent stuff, but not WEB Standard
Document. getElementsByTagName // get the object array of the specified tag Value

The following example shows the effect by removing the comments and running them directly.
Copy codeThe Code 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" style = "width: 100%" value = "exercise">
<Input type = "hidden" name = "organizationId" style = "width: 100%" value = "verify 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>
</Tr>
<Tr>
<Td width = "15%" align = "right"> sub-database encoding: </td>
& Lt; td width = "20%" & gt;
<Select name = "subinventoryCode" style = "width: 100%" id = "subinventoryCode">
<Option value = "QTWL"> QTWL </option>
<Option value = "BTSPT"> BTSPT </option>
<Option value = "BTS"> BTS </option>
</Select>
</Td>
</Tr>
<Tr height = "22">
<Td colspan = "2" align = "center"> <input type = "button" value = "Get specified object" onclick = "do_check ()"> </td>
</Tr>
</Table>
</Form>
</BODY>
</HTML>
<Script language = "JavaScript">
<! --
Function do_check (){
// GetElementById: gets the element through ID, so you can only access the element with the ID set.
// The Return Value of the method ensures 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: obtains the element by NAME.
// The Return Value of the method is an array. Even if there is only one object with the given value as the name attribute in the whole page, it will be returned as an array.
// The length of the array is only one.
// Var organizationId = document. getElementsByName ("organizationId ");
// Alert (organizationId [0]. value );
// Alert (organizationId. length );

// GetElementsByTagName: obtains the element by using the TAGNAME (Tag Name). Of course, a DOCUMENT contains the same tag,
// Therefore, this method is similar to getElementsByName to get an array, but it is only a difference in the way the object is obtained.
// 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 consisting of all tags in the document, including all elements in the document Object.
// Generally, the specified element is obtained by name. However, it is more intelligent than getElementsByName. If one of the objects that meet the conditions is met, this object is returned. Multiple elements are returned as arrays.
// Var organizationId = document. all ["organizationId"];
// Alert (organizationId [0]. value)
// Document. all ["organizationId"]
// Document. all. item ("organizationId ")
}
// -->
</SCRIPT>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.