简体   繁体   中英

dynamic window.find not working with jQuery

I can't for the life of me figure out why this isn't working.

I want to search the current page for text using a search box. I googled and found this: http://www.javascripter.net/faq/searchin.htm . I implemented the code into my site, but it doesn't work. the function ( findString() ) works, but only when I hard-code a string (as in i can't use javascript or jquery to get the value of a text input). I made this fiddle: http://jsfiddle.net/alyda/CPJrh/4/ to illustrate the problem.

You can uncomment different lines to see what I've tested.

jQuery has a method :contains() that will make easier what you are looking for.

Take a look here: fiddle

$("button[type='submit']").click(function () {
    var string = $('#search').val();
    var matched = $('li:contains(' + string + ')');
    matched.css('color','red');
    console.log(matched);
    return false;
});

In firefox I noticed that the fiddle (v4) as given in the question worked, but not in the way the asker expected it to.

What happens in firefox is that the function does find the value..: you have just entered it in the input-field. Then the browser's find method seems to hang in the 'context' of the input 'control' and doesn't break out of it. Since the browser will continue to search from the last active position, if you select anything after the input-field, the function works as expected. So the trick is not to get 'trapped' in the input-field at the start of your search.

A basic (dirty) example on how to break out of it (not necessarily the proper solution nor pure jquery, but might inspire a useful routine, since you now know the root of the problem in FF):

$( "button[type='submit']" ).click(function(){

    var tst=$('#search').val(); //close over value
    $('#search').val('');       //clear input
    if(tst){                    //sanity check
      this.nextSibling.onclick=function(){findString( tst );}; //example how to proceed
      findString( tst );  //find first value
    } else { alert('please enter something to search for'); }
    return false;
});

Example fiddle is tested (working) in FF.

PS: given your specific example using <li> , I do feel Sergio's answer would be a more appropriate solution , especially since that would never run line: alert ("Opera browsers not supported, sorry...") , but the proper answer to your window.find question is still an interesting one!

PS2: if you essentially are using (or replicating) the browser's search-function, why not educate the user and instruct them to hit Ctrl + F ?

Hope this helps!

I found a fix (sort of). It seems that the input needs to be placed well AFTER the content to be searched in the DOM. That means I've done the following:

<section class="content">
  <h2>Fire</h2>
  <h3>Fire Extinguishers</h3>
  <ul>
    <li>Model 240</li>
    <li>Model C352, C352TS</li>
<li>Model C354, C354TS</li>
  </ul>
  ...
  <div id="navbar">
    <ul>
      ...
    </ul>
    <input id="search" type="text" class="form-control pull-left" placeholder="Search for part number">
    <button id="submit" type="submit" class="btn btn-default pull-left" style=" margin-top:6px;">Search</button>
  </div>

as you can see, I've moved the input (which is in the navbar div) BELOW all of the text I want to search, and used CSS to programmatically place the navbar at the top of the page. I don't particularly like this setup (as it messes with the flow of content) but since I was looking for the quickest and simplest implementation of a single-page search, it will have to do.

I would still love to know why this happens, when the javascript is at the end of the DOM where it belongs...

I had same problem in an angularjs app and I fix it by changing DOM structure.
my HTML code was something like this:

<body>
       <div class="content" >
            <input class="searchInput" />
            <p>
                content ....
            </p>
       </div>
</body>

and I changed it to something like this:

<body>
       <div class="search">
            <input class="searchInput" />
       </div>

       <div class="content">
            <p>
                content ....
            </p>
       </div>
</body>

Note: I'm aware that this topic is old.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM