Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call your own method using "this" on a DOM node

Tags:

javascript

I would like to make something like this:

function alrtHtml() {
  alert(this.innerHTML)
}

function myFunc(id) {
  document.getElementById(id).alrtHtml()
}
<html>
<head>
  <meta charset="UTF-8" />
  <title>test</title>
</head>
<body>
  <h1 id="h1">Hello world!</h1>
  <input type="button" value="click" onclick="myFunc('h1')" >
</body>
</html>

The result should be an alert with the text "Hello world!" inside the h1 tag.

It is my goal to be able to do this without explicitly passing the element as an argument to alertHtml.

like image 574
William Stanley Avatar asked Nov 02 '16 18:11

William Stanley


People also ask

How do you call a method in node JS?

The call() method returns the result of calling the functionName() . By default, the this inside the function is set to the global object i.e., window in the web browsers and global in Node. js. Note that in the strict mode, the this inside the function is set to undefined instead of the global object.

How do I access DOM nodes?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable. Logging demoId to the console will return our entire HTML element.

Which is method to search a node from the DOM?

The Selectors API provides methods that make it quick and easy to retrieve Element nodes from the DOM by matching against a set of selectors. This is much faster than past techniques, wherein it was necessary to, for example, use a loop in JavaScript code to locate the specific items you needed to find.


3 Answers

You generally don't want to extend native prototypes, and one way to create chainable methods without doing that, is to create your own method to get the elements, and then create another chainable method to alert the innerHTML, like most libraries do.

Probably the simplest example would be something like this

function getElement(selector) {
  if (!(this instanceof getElement)) return new getElement(selector);
  this.element = document.querySelector(selector);
  return this;
}

getElement.prototype.alertHtml = function() {
  alert(this.element.innerHTML);
  return this;
}

function myFunc(id) {
  getElement(id).alertHtml();
}

myFunc('#test');
<div id="test">TEST</div>

This way you're only extending your own objects, not native objects, and you can create any kind of chainable method to add to that.

like image 158
adeneo Avatar answered Nov 15 '22 00:11

adeneo


What you're looking to do is add your function to the prototype of whatever type document.getElementById(id) returns.

In this case it's returning an Element, so in order to add your function to its prototype you would write the following code.

Element.prototype.alrtHtml = function() {
  alert(this.innerHTML)
}
like image 42
DominicValenciana Avatar answered Nov 15 '22 00:11

DominicValenciana


As another alternative, you could also pass the element right to alertHTML:

function alertHTML(el) {
  alert(el.innerHTML)
}

function myFunc(id) {
  var elArg = document.getElementById(id)
  alertHTML(elArg)
  
  // You could also write it like this:
  /*
  alertHTML(document.getElementById('h1'))
  */
}
<h1 id='h1'>Hello, world</h1>
<button onclick="myFunc('h1')">Button</button>

There's a plethora of reasons to do that, but the gist of it is to avoid having issues if somebody else creates a alertHTML method on the Element prototype.

EDIT: If you really want to use this, you might also like to learn about binding functions - funfunfunction made a good video on this here. Here's how that would work:

function alertHTML() {
  alert(this.innerHTML)
}

function myFunc(id) {
  var el = document.getElementById(id)
  alertHTML.apply(el)
}
<h1 id='h1'>Hello, world</h1>
<button onclick="myFunc('h1')">Button</button>

apply runs whatever its function is with this as the first argument you pass to apply. (The rest of the arguments you pass to apply are passed directly to the function.)

like image 28
Florrie Avatar answered Nov 15 '22 00:11

Florrie