ProgMaster._.

How to Create Highlight Searched Text By Using HTML, CSS & JAVASCRIPT.





This tutorial will guide you through creating a Highlight Searched Text By Using HTML, CSS & JAVASCRIPT. Follow all the given step :

Step 1 : Create Index.html and add given html code on it.
Step 2 : Create style.css and add given css code on it.
Step 3 : Create index.js and add given JavaScript code on it.

HTML


HTML Code for Highlight Searched Text.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Highlight Searched Text</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <input type="text" id="searchInput" placeholder="Search..." oninput="HighlightText()">

        <p id="content">
            In the context of programming and web development, "js" typically refers to JavaScript.
            JavaScript is a high-level, interpreted programming language that is widely used to create
            interactive effects within web browsers. It allows developers to manipulate elements on 
            a web page, respond to user actions, and dynamically update content without needing to 
            reload the entire page.
        <br><br>
            JavaScript is essential for modern web development as it enables the creation of dynamic 
            and interactive user experiences. It can be used both on the client-side 
            (in the web browser) and on the server-side (with Node.js runtime). 
            JavaScript is known for its versatility and is supported by all major web browsers, 
            making it a fundamental skill for web developers.
        </p>
    </div>

    <script src="index.js"></script>
</body>
</html>
    


CSS


CSS Code for Highlight Searched Text.

    
*{
    padding: 0;
    margin: 0;
}
body{
    background: #f6aeff;
    display: flex;
}
.container{
    background: #fef6ff58;
    max-width: 80%;
    margin: 100px auto;
    border-radius: 10px;
    border: 1px solid #ffffff52;
    box-shadow: 0 0 10px #0000001a;
}
.container input{
    outline: none;
    padding: 10px;
    width: 250px;
    border: none;
    height: 10px;
    display: flex;
    margin: 20px auto 0px;
    border-radius: 5px;
}
#content{
    padding: 15px;
    margin-top: 10px;
}
.highlight{
    background-color: yellow;
    font-weight: bold;
}
    


JavaScript


JavaScript code for Highlight Searched Text.

    
function HighlightText() {
    var searchText = document.getElementById('searchInput').value.trim();
    var content = document.getElementById('content');

    var spans = content.querySelectorAll('.highlight');
    spans.forEach(function(span) {
        span.outerHTML = span.innerHTML;
    });

    if (searchText !== '') {
        var regex = new RegExp(searchText, 'gi');
        content.innerHTML = content.innerHTML.replace(regex, function(match) {
            return '' + match + '';
        });
    }
}
    



Download Source Code



What is HTML

HTML, or HyperText Markup Language, is the standard markup language used to create and design documents on the World Wide Web. It's essentially the backbone of web pages, providing the structure and content that web browsers interpret and display.

HTML consists of a series of elements, which are enclosed by tags to define their purpose and function on a webpage. These elements can include headings, paragraphs, images, links, lists, forms, and more. By using a combination of HTML elements, web developers can organize and present information in a structured and visually appealing manner.

HTML is not a programming language but rather a markup language. This means it doesn't have the capability to perform calculations or execute complex logic like programming languages do. Instead, HTML focuses on describing the structure and content of a document, leaving the presentation and functionality to other technologies like CSS (Cascading Style Sheets) for styling and JavaScript for interactivity.

In summary, HTML serves as the foundation for building web pages, providing the structure and content necessary for browsers to render and display information to users on the internet.

What is CSS

CSS, or Cascading Style Sheets, is a language used to style the presentation of web documents written in HTML and XML. It provides a way to control the layout, colors, fonts, and other visual aspects of a webpage, enabling developers to create attractive and responsive designs. By separating content from presentation, CSS promotes cleaner code, easier maintenance, and greater flexibility in website design. With CSS, developers can achieve consistent branding, improved user experience, and better accessibility across various devices and screen sizes.

What is JavaScript

JavaScript is a versatile programming language used for creating dynamic and interactive web content. It enables developers to manipulate HTML elements, respond to user actions, and build seamless web applications. With its extensive ecosystem of libraries and frameworks, JavaScript powers both client-side and server-side development, making it an essential tool for modern web development.