Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Does an HTML element have a prototype (like Element.prototype.myFunc)?

I'm trying to make animations, and I want to be able to do this:

document.getElementById("item").move()

instead of this:

move(document.getElementById("item"))

I tried achieving that with Object.prototype.move, but that didn't work.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A DOM HTMLElement is an Object so it should work, however, an Object is not necessarily an HTMLElement, so you should add that method to HTMLElement.prototype instead:

HTMLElement.prototype.move = function() {
  let position = 0;
  const self = this;

  function render() {
    self.style = `left: ${position++}px`;
    requestAnimationFrame(render);
  }
  
  requestAnimationFrame(render);
}

document.getElementById('square').move();
#square {
  position: fixed;
  height: 20px;
  width: 20px;
  background-color: blue;
}
<div id="square">
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!