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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:40:37+00:00 2026-06-13T10:40:37+00:00

I want to allow the user to enter in a search string and I

  • 0

I want to allow the user to enter in a search string and I want to return the count of how many times that search item is found. I can get this to work if I hard code the search value into the ‘contains’ method, but as soon as I store the search value into a variable it no longer works.

HTML:

<div class="myTest">this</div>
<div class="myTest">this</div>
<div class="myTest">that</div>
<div class="myTest">that</div>
<div class="myTest">this</div>
<div id="howMany"></div>

JAVASCRIPT:

$(function() {
    var myVal= "that";  
    var existingList = $("div:contains(myVal) + .myTest").length;
    $("div[id*='howMany']").html(existingList);
})
  • 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-13T10:40:38+00:00Added an answer on June 13, 2026 at 10:40 am

    A few errors in your code :

    • + in jQuery selectors doesn’t do what you think it does
    • you’re including the string myVal and not the value of myVal in the selector
    • use # to look for an element with a given id

    You could search using

    var existingList = $('div.myTest:contains("'+myVal+'")').length;
    

    But this wouldn’t work with strings containing double quotes. For any kind of string, I would rather suggest

    var existingList = $('div.myTest').filter(function(){
        return $(this).text().indexOf(myVal)>=0;
    }).length;
    

    To issue the number of found results, use

    $("#howMany").html(existingList);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to allow user to enter only english alphabets and characters. How can
I want to allow the user to enter a coupon number in order to
I'm implementing an autocomplete that will allow a user to enter in partial text
I want to allow a user to enter a list of persons in a
I want to create an android application that a user can use to text
I want to allow user to enter characters,numbers and special characters but no the
I want to allow a user to enter a file size, using any of
I want to allow the user, when searching for a Platypus, to enter either
I'm developing a simple little search mechanism and I want to allow the user
I want to only allow the user to enter 0..9, . and backspace in

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.