Jump to content

Highlight text in text input

Recommended Posts

Hi there!

I needed to find a way to highlight text that was entered in the text input, but was not able to find a ready made solution. 
With a bit of research and coding here is a solution that I came up with:


const HTMLTextInputIDs = ['cbParamVirtual1', 'InsertRecordNumber_Field_2']
const highlightedTextBackground = 'yellow';

const generateMimicInputTemplate = (inputId) => {
return `<div contenteditable="true" class="mimic-text-input cbFormTextField" related-input="${inputId}"></div>`;

const addMimicInput = (HTMLInputElelement, HTMLMimicInputMarkup) => {
HTMLInputElelement.insertAdjacentHTML('afterend', HTMLMimicInputMarkup);

const addMimicInputStyles = () => {
.mimic-text-input {

    -webkit-appearance: textfield;
    -moz-appearance: textfield;
    appearance: textfield;
    white-space: nowrap;
    color: transparent;
.mimic-text-input:first-line {
    background-color: ${highlightedTextBackground};

const syncInputWithMimic = (HTMLInputElelement, HTMLMimicInput) => {
HTMLMimicInput.addEventListener('input', (e)=>{
HTMLInputElelement.value = HTMLMimicInput.innerText;

const syncMimicWithForm = (HTMLFormElement, HTMLMimicInput)  => {
HTMLMimicInput.addEventListener('keydown', (e)=>{
if(e.keyCode == 13) {
console.log('enter fired')

const main =  () => {
const HTMLcaspioForm = document.querySelector('#caspioform');

HTMLTextInputIDs.forEach(textInputID => {

const HTMLtextInput = document.querySelector(`#${textInputID}`);  
HTMLtextInput.style.display = 'none';
addMimicInput(HTMLtextInput, generateMimicInputTemplate(textInputID));
const HTMLMimicElement = document.querySelector(`[related-input="${textInputID}"]`);
syncInputWithMimic(HTMLtextInput, HTMLMimicElement);
syncMimicWithForm(HTMLcaspioForm, HTMLMimicElement);

document.addEventListener('DataPageReady', main)


Hope this helps.


You can use it as it is. Just add this code to the header/footer of your form DataPage.
The only thing you need to change is to add your IDs of your text inputs where you need this function to be added. These IDs are store in HTMLTextInputIDs array:
const HTMLTextInputIDs = ['cbParamVirtual1', 'InsertRecordNumber_Field_2']

By analogy, you can add here coma separated IDs of your inputs.

Also, cou can change the background color of the text in highlightedTextBackground variable.

The end result will look like in the screenshot attached.

Hope some will find this useful.

Search form 2022-06-28 14-37-47.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...