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

206
Visualizações
How to add jquery's .html() in vanilla js?

I want to convert jquery to lightweight js. But I get error.

Like I want to use jquery's $('#app').html('test');

var $ = function (method_name) {
return document.querySelector(method_name);
}

$.prototype.html = function (value) {
this.innerHTML = value;
};

$('#app').html('test'); // <--- .html() is not function

How to add .html() in $()?

only $('#app').innerHTML = 'test' is working

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can try do it with class:

class MyJQueryObject {
  constructor(query) {
    this.target = document.querySelector(query);
  }
  
  html(newHTML) {
    if (typeof newHTML !== 'undefined') this.target.innerHTML = newHTML;
    return this.target.innerHTML;
  }
}

const $ = function (query) {
  return new MyJQueryObject(query);
}

const target = $('#target');

console.log(target.html());

target.html('It was easy');
<div id="target">Change me if you can</div>

Also with class you can easily add/remove methods. And the best part that you can define html not as function but as property with the help of getters and setters. For me it is more clearly than check if you pass any argument or not. Example of usage:

class MyJQueryObject {
  constructor(query) {
    this.target = document.querySelector(query);
  }
  
  get html() {
    return this.target.innerHTML;
  }
  
  set html(newHTML) {
    this.target.innerHTML = newHTML;
  }
}

const $ = function (query) {
  return new MyJQueryObject(query);
}

const target = $('#target');

console.log(target.html);

target.html = 'It was easy';
<div id="target">Change me if you can</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Just found alternative jquery js -> CashJS.

The reason why I want to convert jquery to vanilla js is because I am concerned about fast performance and very lightweight js too. CashJS is abit faster than jQuery.

https://github.com/fabiospampinato/cash

about 4 years ago · Juan Pablo Isaza 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