Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

326
Visualizações
How to create a function like `.querySelector(elm)`?

I'm trying to make a function that works like this:

JavaScript:

let elm = document.querySelector('section')
elm.gridify();

assuming the function gridify adds display: grid; to the element.

My question is: is that possible and, if so, how?

I tried this, and it's actually working:

function gridify() {
  this.gridify = function(element) {
    let myElement = document.querySelector(element)

    myElement.setAttribute('style', 'display: grid;')
    console.log(myElement);
  }
}

var test = new gridify();
test.gridify('section');

Now, what I want is to make something like what I wrote above.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

First of all you have to know that edit prototype is something you should never use, but this is the only way to add a custom method to HTMLElement objects (in your case) :

HTMLElement.prototype.gridify = function() { this.style.display = "grid" };

let div = document.querySelector('#myDiv');
div.gridify();

console.log(div.style.display);
// Output
// grid

As suggested in the comments use a function gridify which takes your HTMLElementas parameter is a better practice ;)

function gridify(element) {
    element.style.display = "grid";
}

gridify(div);

console.log(div.style.display);
// Output
// grid
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda