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 😊