• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How to Create Search (Crtl-F) functionality using Aura Component

  • September 1, 2023
  • Tech Blogger
  • Merfantz Developments, Salesforce Lightning
  • 0

Custom Search Bar:

Description:

 We will implement a custom search bar functionality using an Aura component. In Salesforce Lightning Aura components, you can indeed create a search bar functionality that allows users to search and filter data.

It highlights all occurrences of the search term in the content area. The highlighting helps users quickly locate and navigate through the matches.paragraph.

Here’s how the process works:

It handles the logic when the text is entered in search bar. It extracts the search term from the input field, and if the search term is empty, it clears any existing highlighted matches. Then, it calls the search and Highlight helper method to perform the actual search and highlighting.

The helper file contains methods that perform the actual search and highlighting. The searchAndHighlight method is responsible for identifying matches within the content and applying a highlighting CSS class to them. The highlight Match method applies the highlighting to different HTML elements (paragraphs, tables, rows, cells) within the content area.

In Style ‘.match’ class is used to highlight the matches with a yellow background, border radius, and shadow.

The media screen in the CSS define different styles for the search input field based on the screen width. This helps in making the component responsive, with different input field widths for different screen sizes.

Please note:  It’s working for both Classic and Lightning View.

Codes:

Component:

<aura:component  implements=”force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader” access=”global”>
<lightning:input name=”text-12″  placeholder=”Search..” class =”customInput” aura:id=”text-12″ value=””  onchange=”{!c.searchButtonClick}”/>
<div id=”realTimeContents”>
<p>
<br/>

Merfantz Technologies
<br/>
<br/><span class=”space”></span>Note:(Your content insert here…)</p>
</div>
</aura:component>
Controller JS:
(
searchButtonClick: function(component, event, helper) {
var searchTerm = component.find(‘text-12’).get(‘v.value’).trim();
if($A.util.isEmpty(searchTerm)||searchTerm.length == 0){
var realTimeContents = document.getElementById(‘realTimeContents’);
var content = realTimeContents.innerHTML;
content = content.replace(/<span class=”match”>([^<]+)<\/span>/g, ‘$1’);
realTimeContents.innerHTML = content;
}
helper.searchAndHighlight(component, searchTerm);
}
})

Helper JS:
({
searchAndHighlight: function(component, searchTerm) {
if (searchTerm&&searchTerm!=’ ‘) {
var realTimeContents = document.getElementById(‘realTimeContents’);
var content = realTimeContents.innerHTML;
content = content.replace(/<span class=”match”>([^<]+)<\/span>/g, ‘$1’);
realTimeContents.innerHTML = content;
var searchTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, ‘\\$&’);
var searchTerm = searchTerm.replace(‘&’, ‘&amp;’);
var searchTermRegEx = new RegExp(searchTerm, ‘ig’);
var para = document.getElementById(‘realTimeContents’);
var paragraphContent = para.innerHTML;
var matches =paragraphContent.match(searchTermRegEx);//.join(”).split(”);
if (matches != null && matches.length > 0) {
this.highlightMatch(component, searchTermRegEx,searchTerm);
return true;
}
}
},
highlightMatch: function(component, searchTermRegEx,searchTerm) {
var realTimeContents= document.getElementById(‘realTimeContents’);
var pElements = realTimeContents.getElementsByTagName(‘p’);
var tbodyElements = realTimeContents.getElementsByTagName(‘tbody’);
var trElements = realTimeContents.getElementsByTagName(‘tr’);
var tdElements = realTimeContents.getElementsByTagName(‘td’);
function replaceTextContent(match) {
return match.replace(searchTermRegEx, ‘<span class=”match”>$&</span>’);
}
for (var i = 0; i < pElements.length; i++) {
var pContent = pElements[i].innerHTML;
if(!searchTerm.includes(‘&’)){
pContent = pContent.replace(/&amp;/g, ‘&’);
}
pElements[i].innerHTML = pContent.replace(/>([^<]*)</g, replaceTextContent);
}
for (var i = 0; i < tbodyElements.length; i++) {
var tbodyContent = tbodyElements[i].innerHTML;
if(!searchTerm.includes(‘&’)){
tbodyContent = tbodyContent.replace(/&amp;/g, ‘&’);
}
tbodyElements[i].innerHTML = tbodyContent.replace(/>([^<]*)</g, replaceTextContent);
}
for (var j = 0; j < trElements.length; j++) {
var trContent = trElements[j].innerHTML;
if(!searchTerm.includes(‘&’)){
trContent = trContent.replace(/&amp;/g, ‘&’);
}
trElements[j].innerHTML = trContent.replace(/>([^<]*)</g, replaceTextContent);
}
for (var k = 0; k < tdElements.length; k++) {
var tdContent = tdElements[k].innerHTML;
if(!searchTerm.includes(‘&’)){
tdContent = tdContent.replace(/&amp;/g, ‘&’);
}
tdElements[k].innerHTML = tdContent.replace(/>([^<]*)</g, replaceTextContent);
}
realTimeContents.innerHTML =  realTimeContents.innerHTML.replace(/(<\/?a(?:\s[^>]*)?>|<br[^>]*>|[^<]+)/ig, function(match) {
return match;
});
}
})

Style:

.THIS.button{padding-top: 60px;
height: 30%;
width : 100px;
}
.THIS .match {
background-color: #fff34d;
border-radius: 5px;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.7);
padding: 1px 4px;
margin: 0 1px;
}
@media screen and (min-width: 768px) {
.THIS .customInput {
width: 50%;
border-radius: 5px;
background-color: #f2f2f2;
box-shadow: 0 2px 1px rgba(0, 0, 0, 0.7);
margin:auto;
}
}
@media screen and (max-width: 767px) {
.THIS .customInput {
width: 100%;
}
}

Output:

Classic in Desktop –

Desktop View

 

Lightning in mobile –

 

Lightning View Mobile Page

Related Links:

merfantz.com/blog/how-to-use-einstein-search-in-salesforce/

Author Bio

Tech Blogger
+ Recent Posts
  • May 9, 2025
    Mastering Attachment Compression for Salesforce Developers
  • May 2, 2025
    Salesforce API Integrations: Connect with Slack, Zoom, and Teams
  • Guide to Streamlining Sales Success
    April 17, 2025
    Mastering Salesforce CPQ: A Comprehensive Guide to Streamlining Sales Success
  • April 4, 2025
    Unlocking Salesforce Data Cloud: Unify and Activate Customer Data
Tags: custom Search bar in Aura ComponentSalesforce Lightning Development
  • Previous Looking for Salesforce Implementation Services? Discover Merfantz Technologies
  • Next Are Salesforce Aura Components the Right Choice for Your Development Needs?
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Mastering Attachment Compression for Salesforce Developers May 9, 2025
Salesforce API Integrations: Connect with Slack, Zoom, and Teams May 2, 2025
Guide to Streamlining Sales Success
Mastering Salesforce CPQ: A Comprehensive Guide to Streamlining Sales Success April 17, 2025

Copyright @2023 Merfantz Technologies, All rights reserved