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 9007795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:47:05+00:00 2026-06-16T01:47:05+00:00

Using a small plugin to highlight text strings from an input field within a

  • 0

Using a small plugin to highlight text strings from an input field within a form.

JavaScript text higlighting jQuery plugin

I have modified the code slightly to allow the user to add multiple strimgs into the input field by splitting them with a comma which will highlight multiple keywords.

This works great in this instance: stack,overflow,js

However if I was to type in stack,overflow,js, (note the , after the last string) it hangs the browser and becomes unresponsive.

The code I am using is:

$(function() { 
    if ( $("input#searchterm").val().length > 0 ) {
        $("input#searchterm").addClass('marked-purple');
        var arrayOfKeyWords= $("input#searchterm").val().split(',');
        for (var i=0;i<arrayOfKeyWords.length;i++) {
            $('.message p.messagecontent').highlight(arrayOfKeyWords[i]);
        }
    }           
});

Does anyone have an idea of how to ignore the last comma if the user has added it?

Thanks in advance

  • 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-06-16T01:47:07+00:00Added an answer on June 16, 2026 at 1:47 am

    You could do an empty value check before calling highlight(), like this:

    if ($("#searchterm").val().length > 0) {
        $("#searchterm").addClass('marked-purple');
        var arrayOfKeyWords = $("#searchterm").val().split(',');
        for (var i = 0; i < arrayOfKeyWords.length; i++) {
            if (arrayOfKeyWords[i] !== "") { // ensure there is a value to highlight
                $('.message p.messagecontent').highlight(arrayOfKeyWords[i]);
            }
        }
    }
    

    Alternatively you could strip the trailing commas if there are any.

    if ($("#searchterm").val().length > 0) {
        $("#searchterm").addClass('marked-purple');
        var arrayOfKeyWords = $("#searchterm").val().replace(/,+$/, '').split(',');
        for (var i = 0; i < arrayOfKeyWords.length; i++) {
            $('.message p.messagecontent').highlight(arrayOfKeyWords[i]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using a small plugin to highlight some text that a user searches for on
I have an input text containing a default date and I'm using jQuery's Datepicker
I am using small plugin to jquery: jquery-plugin-autoresize But i have trouble with using
I have a small Java/Gradle project. I'm using the Application plugin to create a
I'm using the jQuery maphighlight plugin with a map of the U.S. I have
We are using this excellent plugin for some small sparkline charts. Issue I have
I have a small gradle build which is using a 3rd party plugin it
I'm using Backstretch jQuery plugin to create full screen background images, but have a
I am using jquery tooltip plugin cluetip that is small or very big depending
for this I am using colorbox jQuery plugin http://colorpowered.com/colorbox/ and a form. I am

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.