Javascript uses the element id and name to directly obtain the element.

Source: Internet
Author: User

Javascript uses the element id and name to directly obtain the element.

This article mainly introduces the method for javascript to directly obtain elements through element id and name. It involves some techniques related to javascript to obtain elements and is very useful. For more information, see

This example describes how to use javascript to directly retrieve elements by element id and name. Share it with you for your reference. The specific analysis is as follows:

We know that some third-party js libraries have simplified how to quickly select elements in html. It seems like they are not easy to understand. In addition, js itself comes with a simple Selection Method for special elements. The following is a brief introduction.

In html, the most direct identification of html elements is the name and id attributes. The two are slightly different: the id must be unique on the page, but the name can be repeated.

In js, if the id name is not the same as the built-in attribute or the global variable name, the name will automatically become the attribute of the window object. In the top-level environment of an html page, there are:



This = window

So if we write the following html element code, we can reference it like this:







<Input type = "button" id = "btn_ OK" value = "OK" onclick = "..."/>

// Can be referenced like this

Btn_ OK .onclick = function (){};

// Or the following is the same

Window. btn_ OK .style = ...;

For the name attribute, only some types of html elements have similar methods, such as form, img, iframe, applet, embed, and object. You can use global variables or document attributes to access the elements of a specific name attribute. If multiple elements have the same name attribute, return an object similar to a read-only array of NodeList, for example, the following code:















// We can reference the element whose name is pic as follows:

For (x in pic)

Console. log (pic [x]. alt );

// Or non-"standard" Syntax

For each (img in pic)

Console. log (img. alt );

I hope this article will help you design javascript programs.

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: 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.