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

92
Views
JavaScript or jQuery to select the last x letters in a string

html:

<select id="dropdown">

<optgroup>
<option>11111</option>
<option>22222</option>
</optgroup>

<optgroup>
<option>33333</option>
<option>44444</option>
</optgroup>

</select>

I want to select the last 4 characters of the string in every <option>, then style them as color:white;. But I still need them to be there, just that no one can see.

This is what I have:

let myStr = document.querySelector('#dropdown optgroup  option').innerHTML;
let lastChar = myStr[myStr.length -4];

it only selects the second character in the string, maybe a for loop to select all last 4?

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

0

Expanding on what one of the comments said:

<select id="dropdown">

  <optgroup>
    <option value="11111">1</option>
    <option value="22222">2</option>
  </optgroup>

  <optgroup>
    <option value="33333">3</option>
    <option value="44444">4</option>
  </optgroup>

</select>

Styling won't work anyway (you have very limited options styling select elements), and even if it did, making the colour the same as the background is extremely bad practice. If you must do that sort of thing in the future it's best to manipulate the element itself rather than try to hide it.

about 4 years ago · Juan Pablo Isaza Report

0

You could use the slice method to select the last x characters in a string.

let myStr = document.querySelector('#dropdown optgroup option').innerHTML;
let lastChar = myStr.slice(-4);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

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!