Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Specify an HTML element before the custom function through dot (like element.myFunction)?

So, I have my custom function that I want to do something with the specified element, like:

const element = document.querySelector('some-element')
element.myFunc()

function myFunc() {
//do something with element specified before the dot
}

But this gives me an error that this is not a function. The only thing I can do is to specify the element as an argument of my function, like that:

const element = querySelector('some-element')
myFunc(element)

function myFunc(element) {
   //do something with element
}

What do I need to be able to use my function through the dot, like element.myFunction() ?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You asked to add a function as a property of an element. That's an example:

function myFunc(){
    console.log(this);
}

element.myFunc = myFunc;
element.myFunc();
about 4 years ago · Juan Pablo Isaza Denunciar

0

There's nothing inherently wrong with just passing the element to the function. But if you really want to do this...

querySelector returns an Element. So you could add your function to the Element prototype. For example:

Element.prototype.myFunc = function () {
  console.log(this);
};

const element = document.querySelector('#some-element')
element.myFunc();
<div id="some-element">test</div>

It's worth pointing out though that getting into this habit can potentially lead to issues. As pointed out in a comment below, this "pollutes" every element in the DOM. So it's best to be careful with this approach.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda