jquery - Save previous search on .change() JavaScript -


I am trying to filter for words using the jquery mobile filter feature and are searching the user at the same time I found here with some code for highlights the word.

  $ (document) .ready (function () {jQuery.fn.highlight = function (str, className) {var regex = new RegExp (str, "g"); Return (function () {$ (this) .contents (). Filter (function () {return.nodeType == 3 & amp; amp; regex.test (this.nodeValue);}). (Returns (this.nodeValue || "") .replace (regex, function {match} {return  

My problem is that at the end of many searches, the user wont have been able to highlight their search term because JS has so many people & lt; Spans> function highlights words will not be found.

Is there a way to save the value entered in the search field and then the whole & lt; Span> with that word? I'm using the .change () function to trigger the whole thing, but in this way I can not find the previous value of the input field.

Why not only added tag?

  $ ("# search-basic"). Change (function () {element element} Toggle (); // get rid of span tags; element () (element () ("..."), domNode = elementWithTextToHighlight.get (0); $ ("span.highlight"). DomNode .normalize (); // Add all text nodes to a var search = $ ("input [name = search]"). Val (); elementWithTextToHighlight.highlight (search, "highlight");});  

You should also add a check for the empty string in the highlight script. Otherwise it will add blank term tags for each letter in the text. It can also break functionality.

  jQuery.fn.highlight = function (str, className) {if (str === ") {return this;} // rest function ...}  

  $ (document) .ready (function () {jQuery .fn.highlight = function (str, className) {if (str == = ") {Return this;} var regex = new RegExp (str," gi "); return.each (function () {$ (this) .contents (). Filter (function () {this.nodeType == 3 & amp; amp; regex.test (this.nodeValue);}) replacewith (function () {return (this.nodeValue || ") (rezux, function {match} {returns  
  .highlight {background-color: yellow}  
  & lt; Script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; P & gt; This website has been automatically translated. Please, when you are signing on; It was attacked and charged and against both the leaders and their offices. There is no comment on this website, State Cleta Kas Gibregan, there is no sea jasmine temple, but it works very little, but it is very powerful for work, not for any work. It was attacked and charged and against both the leaders and their offices. State Cleta Casino Gibergen, there is no sea god Sanctuary. & Lt; / P & gt; & Lt; Input ID = "search-base" type = "text" name = "search" />  


Comments