WordPress.org

Ready to get started?Download WordPress

Forums

Highlight Search Terms
[resolved] How To Run Again After AJAX Call (4 posts)

  1. Web2Guru
    Member
    Posted 1 year ago #

    I've looked in to the code, but have not spent enough time with it to figure it out yet. It runs the code that does the highlighting on "jQuery(document).ready()".

    I'm using some AJAX to load some content based on the search term. As expected, the search terms in this content is not highlighted.

    Is there a way to re-trigger the highlighting? Even manually calling a function would work, just some way to have the highlighting run after an an ajax call is run.

    http://wordpress.org/extend/plugins/highlight-search-terms/

  2. RavanH
    Member
    Plugin Author

    Posted 1 year ago #

    I'm afraid that won't be possible without reworking the complete js...

  3. Web2Guru
    Member
    Posted 1 year ago #

    Thanks for the reply, Ravan. I got it working. I pulled some of the code from the plugin script and turned it into a second function (currently added to my themes scripts.js). It's a short function, you could add this to the plugin and call it from within the get_hlst_query function (in place of the original code).

    Here is the new function:

    function ajaxHighlight(hlst_areas,hlst_query) {
    	var area; var i; var s;
    	for (s in hlst_areas){
    		area = jQuery(hlst_areas[s]);
    		if (area.length != 0){
    			for (var l = 0; l < area.length; l++) {
    				for (i in hlst_query){
    					area.eq(l).highlight(hlst_query[i], 1, 'hilite term-' + i);
    				}
    			}
    		}
    	}
    }

    Then just call the function at the end of the ajax post (the variables are already loaded on the page).

    ajaxHighlight(hlst_areas,hlst_query);

    On a side note, I had to remove the break in the for loop because it would not do highlighting in the other "areas" I specified. Not sure what purpose this has here, but it worked perfectly after removing it. Why would you stop it from looping through the other specified areas?

  4. RavanH
    Member
    Plugin Author

    Posted 1 year ago #

    Great! I'll consider this (or something like it anyway, maybe with a handler) for the next release :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic