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

  • 4 months later...

Hi! Just an update - if you would like to highlight search words in the Results Page, you can try these solutions:

1. From this link: https://www.delftstack.com/howto/javascript/highlight-text-using-javascript/

To apply in the DataPage, insert the code below in the Footer of Configure Results Page Fields


document.addEventListener('DataPageReady', function (event) {

let searched = document.querySelector("input[name='Value1_1']").value.trim();
    if (searched !== "") {
        let text = document.querySelector("table[id*='cbTable']").innerHTML;
        let re = new RegExp(searched,"g"); // search for all instances
        let newText = text.replace(re, `<mark>${searched}</mark>`);
    document.querySelector("table[id*='cbTable']").innerHTML = newText;



You can change the Value1_1 based on the form element of your search field.

2. Using mark.js - https://markjs.io/ This is text highlighter written in JavaScript.

To apply in the DataPage, insert the code below in the Footer of Configure Results Page Fields

<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js"></script>


document.addEventListener('DataPageReady', function (event) {

let searched = document.querySelector("input[name='Value1_1']").value;

    if (searched !== "") {
       var instance = new Mark(document.querySelector("table[id*='cbTable']").tBodies[0]);




I hope this helps!

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...