Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6637153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:16:41+00:00 2026-05-25T23:16:41+00:00

I am trying to edit the jQuery hightlight plug in to highlight multiple words.

  • 0

I am trying to edit the jQuery hightlight plug in to highlight multiple words. It works great until you hit the space bar, then it causes FF to freeze in an infinite loop.

FireBug reports that .toUpperCase is not a function, but when I change the same code back, so it is not altering an array element, it’s fine, but does not highlight the two words, only the first one. When the space bar is hit all highlighting goes away.

Here is the what I have so far. The code in question is in the return this.each(function(){}) block at the end:

jQuery.fn.highlight = function(pat) {
function innerHighlight(node, pat) {
    var skip = 0;

    if (node.nodeType == 3) {
        var pos = node.data.toUpperCase().indexOf(pat);
        if (pos >= 0) {
            var spannode = document.createElement('span');
            spannode.className = 'highlight';
            var middlebit = node.splitText(pos);
            var endbit = middlebit.splitText(pat.length);
            var middleclone = middlebit.cloneNode(true);
            spannode.appendChild(middleclone);
            middlebit.parentNode.replaceChild(spannode, middlebit);
            skip = 1;
        }
    } else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
        for (var i = 0; i < node.childNodes.length; ++i) {
            i += innerHighlight(node.childNodes[i], pat);
        }
    }

    return skip;    
}
return this.each(function() {
    var parts = pat.split(' ');
    console.log(parts);
    for (var i in parts) {
        innerHighlight(this, parts[i].toUpperCase());
        console.log("parts["+i+"] >> " + parts[i]);
    }
});
};

Here is the console output in FireBug:

["guy"]                            jquery...ht-3.js (line 46)
parts[0] >> guy                    jquery...ht-3.js (line 49)
parts[i].toUpperCase is not a function
    [Break On This Error] innerHighlight(this, parts[i].toUpperCase());
                                   jquery...ht-3.js (line 48)

any help would be greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:16:41+00:00Added an answer on May 25, 2026 at 11:16 pm

    Oh dear. That plugin is using an unfiltered for...in loop to iterate over an array. That’s bad:

    for...in should not be used to iterate over an Array where index
    order is important. Array indexes are just enumerable properties with
    integer names and are otherwise identical to general Object
    properties. There is no guarantee that for...in will return the
    indexes in any particular order and it will return all enumerable
    properties, including those with non–integer names and those that are
    inherited.

    Because the order of iteration is implementation dependent, iterating
    over an array may not visit elements in a consistent order. Therefore
    it is better to use a for loop with a numeric index when iterating
    over arrays where the order of access is important.

    Where only the properties of the object should be considered, a
    hasOwnProperty check should be performed to ensure that only
    properties of the object and not inherited properties are used
    (propertyIsEnumerable can also be used but is not intuitive).


    So, change this:

    for (var i in parts) {
        innerHighlight(this, parts[i].toUpperCase());
        console.log("parts["+i+"] >> " + parts[i]);
    }
    

    to this:

    for (var i=0; i<parts.length; i++) {
        innerHighlight(this, parts[i].toUpperCase());
        console.log("parts["+i+"] >> " + parts[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run jquery through a rails app. I have the edit
I am trying to edit a string of HTML (from a textarea) using jQuery.
i am trying to intergrate the jquery progress bar but i am getting issues.
I am using jQuery 1.7.1. I am trying to hightlight a column of divs
Hmm... i'm trying to use a php variable in jquery, in my edit.php :
Currently I'm trying to edit a hidden form with the command: jQuery(document).ready(function(){ jQuery(#test).val(hi); });
I'm trying to create a text edit in place myself just by manipulating jQuery
HI, I'm trying to add edit in place functionality to my site with jquery.
I am trying to write my own edit-in-place using jQuery. My code is something
Hello all im new to jquery and im trying to make a edit inplace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.