. JavaScript getElementById ("") detailed __java

Source: Internet
Author: User

1.document.getelementbyid ("") gets an object, with alert showing an "object" instead of a specific value, which has the value and length property, plus ". Value" that gets the exact value.

A. Grammar:

Oelement = document. getElementById ("SID");

Parameters: Where SID: Required, type is a string. return value: Oelement: Object (Element).

Description

object is worthwhile based on the id attribute specified. Returns a reference to the first object of the ID property value equal to the SID. If the corresponding is a set of objects, the first one in the group object is returned. Returns NULL if no object meets the criteria.

B. Examples:

Text1:
<input name= "textName1" type= "text" id= "TextName2"/>
<br>
Text2:
<input name= "textName2" type= "text" id= "textName1"/>
<br>
<input type= "button" Name= "Submit" value= "Text1" Onclick=alert (document.getElementById (' textName2 '). Value)/>
<input type= "button" Name= "Submit2" value= "Text2" Onclick=alert (document.getElementById (' textName1 '). Value)/ >

Enter 1 in the first text box, enter 2 in the second text box, and then click the two buttons below to guess what the result is.

I meant button 1. Returns the value of the first text box, and the button 2 returns the value of the text box. The result is that all two buttons return the value of the first text box. C. Note:

When IE executes document.getElementById (elementname), it is not searched by ID. In IE, getElementById first find name.

There is no such problem in Firefox, when Firefox executes document.getElementById (elementname), only look for objects with IDs equal to ElementName, or null if they do not exist.

It may be that IE is considering compatibility issues to do so.

Focus

Causes the element to focus and executes the code specified by the onfocus event.

getElementById

Gets a reference to the first object of the ID label property to the specified value.

Getelementsbyname

Gets a collection of objects based on the value of the NAME label property.

getElementsByTagName

Gets a collection of objects based on the specified element name.

document.getElementById method Shorthand (JS technique)

One method advocated in Prototype.js is to use the $ () shorthand, and you can use the $ (' ID ') to implement the function of document.getElementById (' ID ') by using the following function.

function $ ()
{
var elements = new Array ();
for (var i = 0; i < arguments.length; i++) {
var element = Arguments[i];
if (typeof element = = ' String ')
element = document.getElementById (element);
if (arguments.length = 1)
return element;
Elements.push (Element);
}
return elements;
}

If you want it to submit an array with multiple IDs, he will also return the array of multiple elements to you.

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.