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

102
Views
Unique CSS styles for "+" and "-" when user types in input

When the user types in + in my input, I would like the + to be green

Ex: +10000 --> "+" should be green and 10000 should be black

When the user types in - in my input, I would like the - to be red

Ex: -10000 --> "-" should be red and 10000 should be black

My idea was to use ::first-letter, but I realize it doesn't work on input

Is this possible at all with css and javascript? Do I need some fancy Regex to accomplish this?

input {
  font-size: 100px;
}

/* only - should be red */
input::first-letter {
  color: red;
}

/* only + should be green */
input::first-letter {
  color: green;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="styles.css" />
    <title>Static Template</title>
  </head>
  <body>
    <input type="text" />
  </body>
</html>

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

0

First get the <input> element with .getElementsByTagName('input')[0], then you can attach an event listener on keyup. From here, you can use .style.color to update the color based on .value[0]:

const target = document.getElementsByTagName('input')[0];
target.addEventListener('keyup', function() {
  if (this.value[0] === '-') {
    this.style.color = 'red';
  }
  else if (this.value[0] === '+') {
    this.style.color = 'green';
  }
  else {
    this.style.color = 'black';
  }
})
input {
  font-size: 100px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="styles.css" />
  <title>Static Template</title>
</head>

<body>
  <input type="text" />
</body>

</html>

Note that the above snippet only checks the first character inputted. If you want to check for any occurrence of the target character, you can loop over the .value.

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!