Search Keyword Highlighting Text Using JavaScript

How to make highlight search keyword text in JavaScript in 3 simple ways?

Search Keyword Highlighting Text Using JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a project which finds a word in the paragraph and highlight it so that the user can see it easily just as we use in chrome using CTRL + F. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to Highlight the Searched Text. Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Highlight Searched Text project. At last, we will use JS (JavaScript) which will add logic to make the Highlight Searched Text project responsive from the user end.

Using CSS we present Search Keyword Highlighting Text Using Javascript projects with source code available for you to copy and paste directly into your own project.

I hope you have got an idea about the project.

HTML Code for Highlighted Text

Below is the HTML Code for Highlighting the Searched keyword using JavaScript.

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Highlight Searched Text</title>
    <!--Google Font-->
    <link href="https://fonts.googleapis.com/css2?family=Rubik&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="wrapper">
            <input type="text" id="text-to-search" placeholder="Enter text to search..">
            <button onclick="search()">Search</button>
        </div>
        <p id="paragraph">
            He ordered his regular breakfast. Two eggs sunnyside up, hash browns, and two strips of bacon. He continued to look at the menu wondering if this would be the day he added something new. This was also part of the routine. A few seconds of hesitation to see if something else would be added to the order before demuring and saying that would be all. It was the same exact meal that he had ordered every day for the past two years.
        </p>
    </div>


    <!--Script-->
    <script src="script.js"></script>
</body>
</html>

First, we’ll start with creating the structure of the Highlight Searched Text project for that as you can see in the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags.

Create Header Using HTML and CSS (Header Source Code)

CSS Code for Highlighted Text

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: "Rubik",sans-serif;
}
body{
    height: 100%;
    background: linear-gradient(
        to right,
        #201d2e 50%,
        #f6f6f6 50%
    ) fixed;
}
.container{
    width: 90vmin;
    background-color: #ffffff;
    padding: 50px 40px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    border-radius: 5px;
    box-shadow: 0 20px 35px rgba(60,60,60,0.2);
}
.wrapper{
    display: flex;
    justify-content: space-between;
}
.wrapper input{
    width: 60%;
    padding: 10px 5px;
    border-radius: 3px;
    border: 1px solid #201d2e;
}
.wrapper button{
    width: 30%;
    background-color: #201d2e;
    border: none;
    outline: none;
    cursor: pointer;
    color: #ffffff;
    border-radius: 3px;
}
.container p{
    line-height: 35px;
    text-align: justify;
    margin-top: 30px;
}
mark{
    background-color: #ffdd4b;
}

Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the Highlight Searched Text project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

Portfolio Website Using HTML CSS And JAVASCRIPT ( Source Code)

JavaScript Code for Highlighted Text

Below is main code of Javascript that is used to highlight the searched keyword.

// Characters to be escaped [.*+?^${}()|[\]\\] 

function search(){
    let textToSearch = document.getElementById("text-to-search").value;
    let paragraph = document.getElementById("paragraph");
    textToSearch = textToSearch.replace(/[.*+?^${}()|[\]\\]/g,"\\
amp;"); let pattern = new RegExp(`${textToSearch}`,"gi"); paragraph.innerHTML = paragraph.textContent.replace(pattern, match => `<mark>${match}</mark>`) }

Here mark() is used to highlight the searched text.

The last stage of the project the JavaScript which we added the logic and coded as per the requirement with some conditions. Let us see the Final Output of the project Search Keyword Highlighting Text using JavaScript.

Final Output Search Keyword Highlighting Text

Below we’ve provided a preview of the codepen that demonstrates the Final output of the search keyword highlighted text.


We have successfully created our Search Keyword Highlighting Text using javascript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on Google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Thank You And Happy Learning!!!

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply