0

I have a search box at the top and a bunch of text at the body section, i want to create a search, like I type something in search box, it should start searching in the body section, how this can be achieved?

Here is my HTML:

<div class="view-mail">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, 
</div>

<div class="input-append">
    <input type="text" class="sr-input" id="insideMail" placeholder="Search Mail">
    <button class="btn sr-btn searchinside" type="button"><i class="fa fa-search"></i></button>
</div>

starting as

$(".searchinside").click(function(e) {
    var key = $("#insideMail").val();
});
Dan
  • 9,391
  • 5
  • 41
  • 73
jangun
  • 31
  • 6

1 Answers1

0

You can use keypress event of particular textbox for that.

$('#search_text').bind("keypress", function(e) {
    var searchText = $("#search_text").val().trim();
    if($(#your-div).indexOf(searchText) >= 0){
        //searched text.. do something
    }
});

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • well this is confusing, where the `yourdiv` is referred to which one and i understand `search_text` might be my input field. What is `SearchText` then – jangun Sep 04 '14 at 14:55
  • `yourdiv` will be the div within which you want to search. And `searchText` will be text entered in your `#search-text` textfield. @jangun – MysticMagicϡ Sep 05 '14 at 04:32
  • i tried: Here is the Error, I am adding here: `TypeError: $(...).indexOf is not a function if($(".view-mail").indexOf(searchText) >= 0){ ` – jangun Sep 05 '14 at 12:22
  • and i want to highlight the found text, whatever the times they appear – jangun Sep 05 '14 at 12:23
  • Try `if($(".view-mail").val().indexOf(searchText) >= 0){` Sorry for typo. :) @jangun – MysticMagicϡ Sep 05 '14 at 12:23
  • And for highlighting, you can refer [this](http://stackoverflow.com/a/987376/1777090) @jangun – MysticMagicϡ Sep 05 '14 at 12:33
  • ok, i tried like this: `$('#insideMail').bind("keypress", function(e) { var searchText = $("#insideMail").val().trim(); if($(".view-mail").val().indexOf(searchText) >= 0){ SelectText($(".view-mail").text()); } });` – jangun Sep 05 '14 at 12:50
  • getting this error: TypeError: Argument 1 of `Range.selectNodeContents is not an object. range.selectNodeContents(text);` – jangun Sep 05 '14 at 12:51
  • i referred u link you gave me and use the function of selectText – jangun Sep 05 '14 at 12:52