Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Shorthand for getElementById

Tags:

javascript

Is there any shorthand for the JavaScript document.getElementById? Or is there any way I can define one? It gets repetitive retyping that over and over.

like image 925
tew Avatar asked Jun 18 '11 20:06

tew


People also ask

What is the getElementById in Javascript?

The getElementById() is a DOM method used to return the element that has the ID attribute with the specified value. This is one of the most common methods in the HTML DOM and is used almost every time we want to manipulate an element on our document. This method returns null if no elements with the specified ID exists.

What is getElementById () value?

The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM. It is used almost every time you want to read or edit an HTML element.


1 Answers

var $ = function( id ) { return document.getElementById( id ); };  $( 'someID' ) 

Here I used $, but you can use any valid variable name.

var byId = function( id ) { return document.getElementById( id ); };  byId( 'someID' ) 
like image 174
user113716 Avatar answered Sep 29 '22 20:09

user113716