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

93
Views
javascript styling element with custom function

I want to style element in javascript easily but this function doesn't work

const addStyles = function (el, styles) {
    Object.assign(el.style, styles);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to get the element first, then access the style property (it's a string) and set your CSS there.

Here's an example. As you can see the text is black and there is no CSS, however, with the javascript line I can make it red.

function addStyles(element, style) {
  element.style = style;
}


const myElement = document.getElementById("myText");

addStyles(myElement, "color: red; font-size: 25px;");
<p id="myText">Some text<p>

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!