0

Is it possible calling element in function if I write that element before the function, example:

let demo = document.getElementById("demo")
demo.giveMeElement()

function demo() {
  // How can I get that #demo element here
}
<div id="demo"></div>
  • 1
    Does [this](https://stackoverflow.com/a/4670387/9074272) answers your question? – ciekals11 Jul 02 '21 at 07:40
  • 1
    Does this answer your question? [How to add my own methods to HTMLElement object?](https://stackoverflow.com/questions/4670361/how-to-add-my-own-methods-to-htmlelement-object) – ciekals11 Jul 02 '21 at 07:41
  • 1
    While it is possible. You are using the same name for both. Choose a different name for your element and use it inside the function. – Tushar Shahi Jul 02 '21 at 07:48
  • `HTMLElement.prototype.doHello = function(thing){ alert('Hello World from ' + thing); }; header_img.doHello();` - it doesn't really work... –  Jul 02 '21 at 07:49
  • Yes, of course, As I understand your question, `demo` is a global variable for the function `demo`. So you can access `#demo` element through `demo` variable and do whatever the operations which are allowed with it. @TusharShahi suggestion is also should be considered. – ThilankaD Jul 02 '21 at 07:49

0 Answers0