JavaScript Trucos:
document.getElementById

[:es]Al programar con el DOM usamos frecuentemente la función document.getElementById, podemos reducir el código convirtiéndolo en una función con un nombre mas corto y fácil de recordar:
gEle=function(a){return ((typeof a==="string")?document.getElementById(a):a);};

Parametros

Esta función solo necesita la cadena que es el id del objeto y devuelve el elemento del DOM con ese id.

Ejemplo de uso

En nuestro código html insertamos un DIV con id «gEleSamp»
<div id="gEleSamp" style="border:solid 1px #ccc;">sample div</div>
El resultado de nuestro código:
sample div
Accedemos al objeto usando su id con gEle y cambiamos el color del texto a rojo:
<span onclick="gEle('gEleSamp').style.color='#f00'">Test </span>
Prueba el código pulsando el siguiente enlace:Test [:en]When programming with the DOM the function document.getElementById function is frequently used, we can reduce the code making it a function with a name shorter and easier to remember:
gEle=function(a){
return ((typeof a==="string")?document.getElementById(a):a);
};

Parameters

This function only needs the string of the id of the object and returns the dom element with that id.

Sample usage

In our html source code we insert a DIV element with id «gEleSamp»
<div id="gEleSamp" style="border:solid 1px #ccc;">sample div</div>
The result of our code:
sample div
We access to the object with gEle using their id and we change the font color to red:
<span onclick="gEle('gEleSamp').style.color='#f00'">Test </span>
Test the code pressing the following link:Test[:]

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *