0

I am seeing plenty of libraries and plugins for finding a search term in a regular page, but they are not written to handle highlighting a string within ajax response content. E.g. many scripts I find do the call to the main function like for example:

`returnDataSearchTermHighlightified = highlight(searchString);`

(^^^ from the accepted answer at: Highlight search terms (select only leaf nodes))

...where that highlight() function's missing second param is auto-populated with the entire HTML body of the page. But in my case I am not wanting to highlight keywords in the parent body, but in the ajax response content only (a div I populate in the parent body), and so I cannot do e.g. this:

returnDataSearchTermHighlightified = highlight(searchString);

...and if I try this:

...
var jqxhr = $.ajax(

        {
            type: 'POST',
            url: ajaxWorkerSourceUrl,
            data: form_data,
            timeout: 15000
        }
    )
    .success(function(returnData) {
        $("#ajaxSpinner").fadeOut('slow');
        searchString = 'example';
        returnDataSearchTermHighlightified = highlight(searchString, returnData);

...then the page seems to load my ajax response with no error, but that response content is empty! Can anyone point me to fixing this? .. or even to a library or plugin designed to handle keyword highlighting in jquery ajax response content?

Thanks!

Community
  • 1
  • 1
govinda
  • 1,683
  • 5
  • 20
  • 34

1 Answers1

1

It seems that function highlight(term, base) requires base to be of type Element or jQuery .

Try

.success(function(returnData) {
        $("#ajaxSpinner").fadeOut('slow');
        var searchString = 'example',
            base = $(returnData);
        highlight(searchString, base);
        base.appendTo(document.body); //whatever you were going to do..
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
  • hmm.. I do try that and I am getting the same result - that my target div in the parent just populates with nothing at all (no error). – govinda Jan 15 '13 at 10:15
  • 1
    Check `base.length` before and after highlight function call. Like this `console.log(base.length)` or better `console.log(base.html())` – Yury Tarabanko Jan 15 '13 at 10:22
  • your tip made me think and look at the code in a way that led me to the answer (anyway it works now for me). As you said I needed to be using: `base = $(returnData);` instead of `base = returnData;` ...with: `returnDataSearchTermHighlightified = highlight(searchString, base);` ...and then the other thing that was killing it (and baffles me that was missing from the accepted answer, here: http://stackoverflow.com/a/3241437/530006 ), was some return output from the `highlight(term, base) {}` function. See my comment to that answer. – govinda Jan 16 '13 at 05:32