Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the method document.getElementById defined?

Note: This is only for learning purpose..

console.log(this);
document.getElementById = function(){
    alert('testing');
}

document.getElementById('someID');

I have the above piece of javascript code written. When I load the page it shows an alert box saying 'Testing'.

So I am guessing getElementById is a method of the document object and I've overwritten it to alert('testing'), which is why its showing me the alert box when the page loads.

If that part is correct, shouldn't I see the getElementById when I expand the document object below? Am I looking for it in the wrong place or something?

enter image description here

like image 742
psj01 Avatar asked Sep 28 '17 21:09

psj01


People also ask

How would you define document getElementById?

Document.getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

Is document getElementById () JavaScript?

Introduction to JavaScript getElementById() methodThe document. getElementById() method returns an Element object that represents an HTML element with an id that matches a specified string. If the document has no element with the specified id, the document. getElementById() returns null .

What is the function of getElementById () *?

The getElementById() method returns the elements that have given an ID which is passed to the function. This function is a widely used HTML DOM method in web designing to change the value of any particular element or get a particular element. If the passed ID to the function does not exist then it returns null.

How do I know if file getElementById exists?

Use the getElementById method to get an element by id if it exists, e.g. if (document. getElementById('my-id') !== null){} . The method returns the element whose id matches the provided string.


2 Answers

Your method is in there. It's just Chrome decided not to show you.

Whether you should do this, however, is another question of its own.

like image 90
Derek 朕會功夫 Avatar answered Nov 09 '22 09:11

Derek 朕會功夫


What you are doing is overriding the document.getElementById() function with your own implementation. The logger just doesn't show all the properties of the document element. Assigning it to a new object does the trick:

document.getElementById = function() {
  console.log('test');
}

var doc = Object.assign({}, document);
console.log(doc);
console.log(doc.getElementById);
.as-console-wrapper {
  max-height: 100% !important;
}

An important note

I would strongly recommend you do not replace the getElementById() functionality with your own, as it can lead to all sorts of trouble. It would be far better to extend the document object with a custom function, instead.

like image 32
Angelos Chalaris Avatar answered Nov 09 '22 10:11

Angelos Chalaris