Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

document.getElementById

Este articulo necesita una revisión editorial. Cómo puedes ayudar.

Esta traducción está incompleta. Por favor, ayuda a traducir este artículo del inglés.

 

Devuelve una referencia al elemento por su ID.

Sintaxis

elemento = document.getElementById(id);

Parametros

  • elemento hace referencia a un Elemento de un objeto, si un elemento con ID especificado no está presente en el documento se declarará nulo.
  • id is a case-sensitive string representing the unique ID of the element being sought.

Ejemplo

 <!DOCTYPE html>
<html>
<head>
  <title>getElementById example</title>
  <script>
  function changeColor(newColor) {
    var elem = document.getElementById("para1");
    elem.style.color = newColor;
  }
  </script>
</head>
<body>
  <p id="para1">Some text here</p>
  <button onclick="changeColor('blue');">blue</button>
  <button onclick="changeColor('red');">red</button>
</body>
</html>

Some text here

bluered

Notes

New users should note that the capitalization of 'Id' in the name of this method must be correct for the code to function - 'getElementByID' does not work, however natural it may seem.

If there is no element with the given id, this function returns null. Note that the ID parameter is case-sensitive, so document.getElementById("Main") will return null instead of the element

because "M" and "m" are different for the purposes of this method.

 

Elements not in the document are not searched by getElementById. When creating an element and assigning it an ID, you have to insert the element into the document tree with insertBefore or a similar method before you can access it with getElementById:

var element = document.createElement("div");
element.id = 'testqq';
var el = document.getElementById('testqq'); // el will be null!

Non-HTML documents. The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "id" are not of type ID unless so defined in the document's DTD. The id attribute is defined to be of ID type in the common cases of XHTML, XUL, and other. Implementations that do not know whether attributes are of type ID or not are expected to return null.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 1.0 (1.7 or earlier) 5.5 7.0 1.0
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 1.0 1.0 (1.0) 6.0 6.0 1.0

Specification

getElementById was introduced in DOM Level 1 for HTML documents and moved to all documents in DOM Level 2.

See also

  • document reference for other methods and properties you can use to get references to elements in the document.
  • document.querySelector() for selectors via queries like 'div.myclass'
  • xml:id - has a utility method for allowing getElementById to obtain 'xml:id' in XML documents (such as returned by Ajax calls)

Etiquetas y colaboradores del documento

Etiquetas: 
 Colaboradores en esta página: pclifecl, OLiiver, fscholz, teoli, tuxisma, Juan c c q
 Última actualización por: pclifecl,