Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am showing few questions and answer in one webform using DIV and p tag. for example:

<div class="inner">
<h4><span class="blue"> Microsoft</span>RELATED QUESTIONS</h4>

<p class="f1">Q) Who is  Microsoft</p>
<p class="f2">

Microsoft Corporation is an American multinational corporation headquartered in Redmond, Washington, that develops, manufactures, licenses, supports and sells computer software, consumer electronics and personal computers and services. Its best known software products are the Microsoft Windows line of operating systems, Microsoft Office office suite and Internet Explorer web browser. Its flagship hardware products are Xbox game console and the Microsoft Surface series of tablets. It is the world's largest software maker measured by revenues.[4] It is also one of the world's most valuable companies.[5]

Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975 to develop and sell BASIC interpreters for Altair 8800. It rose to dominate the personal computer operating system market with MS-DOS in the mid-1980s, followed by the Microsoft Windows. The company's 1986 initial public offering, and subsequent rise in its share price, created an estimated three billionaires and 12,000 millionaires from Microsoft employees. Since the 1990s, it has increasingly diversified from the operating system market and has made a number of corporate acquisitions.

.</p>

</Div>

<input type="text" runat="server"/>


so now i have textbox on right corner, how to search text in the questions and answer on browser using javascript client side. for exmaple, if i type Microsoft, It has to find all Microsoft word which is existing on browser. ( like how CTRL + F works on browser)
Posted
Updated 21-Jan-14 23:42pm
v2

 
Share this answer
 
Comments
Karthik_Mahalingam 22-Jan-14 9:54am    
5!!! good resource.
JoCodes 22-Jan-14 14:27pm    
Thanks karthik for the support. :)
XML
<script src="Web/Scripts/jquery-1.10.2.js"></script>

<script type="text/javascript">
    jQuery.fn.highlight = function (str, className) {
        var regex = new RegExp(str, "gi");
        return this.each(function () {
            $(this).contents().filter(function () {
                return this.nodeType == 3;
            }).replaceWith(function () {
                return (this.nodeValue || "").replace(regex, function (match) {
                    return "<span class=\"" + className + "\">" + match + "</span>";
                });
            });
        });
    };
    var previousElement ;
    function SearchText(element)
    {
        if (previousElement != element)
        {
            previousElement = element;

            $(".highlightWord").contents().unwrap();

        }

        $("#searchable *").highlight("" + element + "", "highlightWord");

    }



      </script>
 
Share this answer
 
Comments
Praveen Tiwari 22-Jan-14 9:32am    
Include the above script in the body and get to work below is the complete html that worked for me
XML
<html><head><title></title>
<style type="text/css">
.highlightWord { background-color: #ff2; }
</style>

</head>
<body>

<div id="searchable">
<p class="f1"> Q) Who is Microsoft </p>
<p class="f2">

Microsoft Corporation is an American multinational corporation headquartered in Redmond, Washington, that develops, manufactures, licenses, supports and sells computer software, consumer electronics and personal computers and services. Its best known software products are the Microsoft Windows line of operating systems, Microsoft Office office suite and Internet Explorer web browser. Its flagship hardware products are Xbox game console and the Microsoft Surface series of tablets. It is the world's largest software maker measured by revenues.[4] It is also one of the world's most valuable companies.[5]

Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975 to develop and sell BASIC interpreters for Altair 8800. It rose to dominate the personal computer operating system market with MS-DOS in the mid-1980s, followed by the Microsoft Windows. The company's 1986 initial public offering, and subsequent rise in its share price, created an estimated three billionaires and 12,000 millionaires from Microsoft employees. Since the 1990s, it has increasingly diversified from the operating system market and has made a number of corporate acquisitions.

.

</p>
    <input id="textSearch" type="text"  />
    <input id="click" type="button" value="Search" onclick="SearchText($('#textSearch').val());" />
</div>
<script src="Web/Scripts/jquery-1.10.2.js"></script>

<script type="text/javascript">
    jQuery.fn.highlight = function (str, className) {
        var regex = new RegExp(str, "gi");
        return this.each(function () {
            $(this).contents().filter(function () {
                return this.nodeType == 3;
            }).replaceWith(function () {
                return (this.nodeValue || "").replace(regex, function (match) {
                    return "<span class=\"" + className + "\">" + match + "</span>";
                });
            });
        });
    };
    var previousElement ;
    function SearchText(element)
    {
        if (previousElement != element)
        {
            previousElement = element;

            $(".highlightWord").contents().unwrap();

        }

        $("#searchable *").highlight("" + element + "", "highlightWord");

    }



      </script>
</body></html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900