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

271
Views
How can I ignore white space when using classList.add?

I'm trying to add classes to an element in JS using classList.add(), if I have input like this ("one two") separated by whitespace how can I take each part as a class ?

let element = document.querySelector(".element")
element.classList.add("one two")
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const str = 'one two three';
const classes = str.split(' ');
document.querySelector(".element").classList.add(...classes);
about 4 years ago · Juan Pablo Isaza Report

0

Add multiple classes into an element:

element.classList.add("one", "two"); 

or

element.classList.add("two"); 
element.classList.add("one"); 

To use classes from an input, seperated by space:

const userInput = "one two";
element.classList.add(...userInput.split(" "));
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!