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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:14:59+00:00 2026-05-24T16:14:59+00:00

I got this code: Link I can highlight the chars of static text by

  • 0

I got this code:
Link

I can highlight the chars of static text by this js:

$("li:contains('Content1')").each(function(){
    var content = "Content1";
    this.innerHTML = this.innerHTML.replace(content,"<span>"+content+"</span>")
})

The Content1 by a dynamic value : $("#main").val()

QUESTION: How can i highlight words found with :contains and with the value from the input box

  • 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-24T16:15:00+00:00Added an answer on May 24, 2026 at 4:15 pm

    This is going to be a little dicey, but check this out.

    Your issue was that as you adjusted the content, you’d no longer be able to check what the original content was. The best way around this is, at the beginning, store the original LI text in a data-attribute, and then use that for manipulation later on.

     $("li").each(function() {
       $(this).attr( 'data-original', $(this).text() );
       $(this).attr( 'data-original-l', $(this).text().toLowerCase() );
     });
    

    We then need to adjust your selector to check this attribute (as opposed to the text value):

     $("li[data-original *= \"" + $("#main").val().toLowerCase() + "\"]")
    

    which, e.g. will search li[data-original*=”Harry Potter”], which is “any LI with an attribute “data-original” which contains “Harry Potter” (see here).

    From there, we just need to tweak your replacement statement as follows:

     $(this).html($(this).data('original').replace(new RegExp(content, "i"), "<span class='highlight'>$&</span>"));
    

    We are doing the same thing you had originally, but now using the $(this).data(‘original’) value instead of $(this).val(). We are also using a Regular Expression so that we can do the replacement case-insensitive. Also, we replace with $& which contains the match from the RegExp test in the original string.

    Easy as pie! Hope this works for you and let me know if you run into any snags.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code: <html> <head> <link type=text/css href=css/blitzer/jquery-ui-1.7.2.custom.css rel=stylesheet /> <script type=text/javascript src=js/jquery-1.3.2.min.js></script>
I've got this code that's changing a picture and additional link with it. var
Hi i wanted to remove a link and i got this code $(document).ready(function(){ $(.features-list
I got this code in order to build an url for the link using
I got this code for a parser from this link. http://www.warriorpoint.com/blog/2009/07/19/android-simplified-source-code-for-parsing-and-working-with-xml-data-and-web-services-in-android/ I am getting
I've got this piece of code: <?php $userid = $me['id']; function showifuserexists() { ?>
I got this code to find text in a string that looks like links
Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;
I got this code from the wordpress <head profile=http://gmpg.org/xfn/11> What does this means? what
I got this code ` // // prints out Hello World! // hello_world(); //First

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.