JavaScript hack which might not know

Nitish Kumar Singh
Nitish Kumar Singh
Clock Image 1 minutes
Posted on
March 10, 2020

JavaScript hack which might not know

Recently, I shared one of my hack with my friends and they said wow 🤩. Earlier I used to think this is very common and most of the JavaScript developers are aware of this.

The hack is realted to the window object of browser.

Some HTML element of webpage are available in window object. Let’s understand with example

<h1 id="title"> This is title </h1>
var ele = document.getElementById('title');
ele.innerHTML = " This is modified Title";

If I say you that you can access the the above HTML element without doing document.getElementById('title'). Some of you might say using jQuery and some of you might say not possible and some might be knowing the hack also.

I will not be using the jQuery. I am going to use the window object to get the element.

alternative of getElementById? #

This is the alternative of getElementById

window.title.innerHTML = " This is modified Title";

Every html element which has id attributes is available in the window object.

I hope you learned something new today! Follow me to get more interesting 🧐 things about JavaScript.

If you leaned then please do share and tweet about this.

Thanks 😊

Last updated at Saturday, May 9, 2020 by Nitish Kumar Singh
comments powered by Disqus

Subscribe to our Newsletter

Tweet this article Tweet this article