No document. getElementByName Method

Source: Internet
Author: User

The first statement is:
The document. getElementByName method does not exist. Document. getElementsByName obtains the tag array.
Document. getElementId: a tag.
<Form name = "form_write">
<Input name = "content" type = "text">

However, it can be obtained in a simple way, such:

Var fn = document. getElementsByName ("form_write") [0]; // get the object under this form
Fn. content. value; // you can use this object directly.
Document. getElementById 1. getElementById

Purpose: Generally, the ID on the page is unique and used to locate an element.
Syntax: document. getElementById (id)
Parameter: id: Required String)
Return Value: object. Return the first object of the same id in the order displayed on the page. If no object meets the conditions, return null.

Example:
Copy codeThe Code is as follows:
Document. getElementById ("id1"). value;


2. getElementsByName

Purpose: query by element name. An array with the same name is returned.
Syntax: document. getElementsByName (name)
Parameter: name: Required String)
Returned value: array object. If no matching object exists, an empty array is returned, in the order displayed on the page.
Note: The returned array value is the value of the value attribute,
If a tag does not have a value attribute, after you add the value Attribute and assign a value, getElementsByName can also get its value,
If no value is assigned to the value attribute, the array value returned by getElementsByName is undefined,
However, you can still obtain the number of document. getElementsByName (name). length Tags with the same name.
If the name attribute is not set, document. getElementsByName can still be used. It will get the value of the value Attribute Based on your id.

Example:
Copy codeThe Code is as follows:
Document. getElementsByName ("name1") [0]. value;
Document. getElementsByName ("name1") [1]. value;
<Span id = 'cbylawindexname' class = 'normalnode' value = 'all'> all </span>"
<Span id = 'cbylawindexname' class = 'normalnode' value = 'all'> ALL </span>"

The span tag does not have the name and value attributes.
However, document. getElementsByName ("CBylawIndexName") will still get the value of value.

3. getElementsByTagName

Purpose: query by HTML Tag name. An array with the same tag element is returned.
Syntax: object. getElementsByTagName (tagname) object can be document or event. srcElement. parentElement.
Parameter: tagname: the required parameter is a String, which is retrieved Based on HTML tags.
Returned value: array object. If no matching object exists, an empty array is returned, in the order displayed on the page.

Example:
Copy codeThe Code is as follows:
Document. getElementsByTagName ("p") [0]. childNodes [0]. nodeValue;
Document. getElementsByTagName ("p") [1]. childNodes [0]. nodeValue;

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.