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.


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


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


Using a jQuery object
Change the color of the first paragraph of the webpage to red
Change style to object with id=”div1″
<div id="div1">
Change color to red
<a href="gEle('div1').style.color='red';">Press to change color</a>

Leave a Reply

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