gEle: shortcut for document.getElementById

Shortcut for the document.getElementById function, it helps to reduce the code and is easier to remember than the full name.

Code

gEle = function(a){
	return ( (typeof a==="string") ? document.getElementById(a) : a );
};

Parameters

a (string of text or object)
The string with the id of the object or one DOM object, this second option is for the use with jQuery

Returned value

The DOM object with the given id or null if not exist

Samples

Using a jQuery object
Change the color of the first paragraph of the webpage to red
p1=jQuery("p")[0];
gEle(p1).style.color="red";
Change style to object with id=”div1″
<div id="div1">
Cambiar color a rojo/Change color to red
</div>
<a href="gEle('div1').style.color='red';">Pulsar para cambiar color/press to change color</a>

Leave a Reply

Your email address will not be published. Required fields are marked *