How to write shorthand for document.getElementById() method in JavaScript ?

The task is select the elements with id by using the shorthand for document.getElementById with the help of JavaScript. we’re going to discuss few techniques.
Approach 1:

  • Define a function that returns the document.getElementById(‘idName’).
  • This method takes IDName as the first and only argument.
  • Call the method with ID as the argument.

Example 1: In this example, a function is created which returns the document.getElementById(‘idName’).

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to write shorthand for 
        document.getElementById()
        method in JavaScript ?
    </title>
</head>
  
<body style="text-align:center;">
      
    <h1 style="color:green;"
        GeeksForGeeks
    </h1>
      
    <p id="GFG_UP" style=
        "font-size: 19px; font-weight: bold;">
    </p>
      
    <button onclick="GFG_Fun()">
        click here
    </button>
      
    <p id="GFG_DOWN" style=
        "color: green; font-size: 24px; font-weight: bold;">
    </p>
      
    <script>
        var ID = function(elementId) {
            return document.getElementById(elementId);
        };
          
        var el_up = ID("GFG_UP");
        var el_down = ID("GFG_DOWN");
          
        el_up.innerHTML = "Click on the button to select"
                    + " the element by shorthand instead"
                    + " of getElementById.";
  
        function GFG_Fun() {
            el_down.innerHTML = "Element selected by "
                    + "shorthand of getElementById";
        }
    </script>
</body>
  
</html>

chevron_right


Output:



  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • Define a prototype = document.getElementById.
  • Use this prototype by passing IDName as the first and only argument inplace of document.getElementById.

Example 2: In this example, a HTMLDocument Prototype is created which then used to select the element by IDName.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>
<html>
  
<head>
    <title>
        How to write shorthand for 
        document.getElementById()
        method in JavaScript ?
    </title>
</head>
  
<body style="text-align:center;">
      
    <h1 style="color:green;"
        GeeksForGeeks
    </h1>
      
    <p id="GFG_UP" style=
        "font-size: 19px; font-weight: bold;">
    </p>
      
    <button onclick="GFG_Fun()">
        click here
    </button>
      
    <p id="GFG_DOWN" style=
        "color: green; font-size: 24px; font-weight: bold;">
    </p>
      
    <script>
        HTMLDocument.prototype.e = document.getElementById;
        var el_up = document.e("GFG_UP");
        var el_down = document.e("GFG_DOWN");
          
        el_up.innerHTML = "Click on the button to select"
                + " the element by shorthand instead of "
                + "getElementById.";
  
        function GFG_Fun() {
            el_down.innerHTML = "Element selected by "
                    + "shorthand of getElementById";
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.