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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:29:21+00:00 2026-06-16T03:29:21+00:00

I am using JSP with HTML. In HTML, I have a textbox and a

  • 0

I am using JSP with HTML.

In HTML, I have a textbox and a multiple select element. I would like to filter the options in the select list as the user types in the textbox.

I found a useful JQuery code on this stackoveflow page:
How to dynamic filter options of <select > with jQuery?

Here’s the code:

<script>
jQuery.fn.filterByText = function(textbox) {
    return this.each(function() {
        var select = this;
        var options = [];
        $(select).find('option').each(function() {
            options.push({value: $(this).val(), text: $(this).text()});
        });
        $(select).data('options', options);

        $(textbox).bind('change keyup', function() {
            var options = $(select).empty().data('options');
            var search = $.trim($(this).val());
            var regex = new RegExp(search,"gi");

            $.each(options, function(i) {
                var option = options[i];
                if(option.text.match(regex) !== null) {
                    $(select).append(
                        $('<option>').text(option.text).val(option.value)
                    );
                }
            });
        });
    });
};

</script>

You could use it like this:

<script>
$(function() {
    $('select').filterByText($('input'));
});
</script>

The problem is that this code only works when the options in the select element is hard coded.
By hard coded I mean like this:

<option value="something">something</option>
<option value="something">something</option>

However, in my case, I have a list of around 6000 values that I am adding to the select list using a java code. Here’s that java code:

<%
 ArrayList<String> allGenes= new ArrayList<String>();
   allGenes.addAll(tree.getAllGenes()); 

     %>

      <%
       for(int i=0; i<allGenes.size(); i++)
       { %>
       var gene=document.createElement('option');
       var str="<%=allGenes.get(i)%>";
       gene.text= str;
       try
        {
            document.getElementById("inputSet1").add(gene,null);
        }
         catch(ex){
                  document.getElementById("inputSet1").add(gene);                             
         }
       <%}%>         

The JQuery code that filters the elements in the select list doesn’t filter these values that i add using java code. Instead, it deletes all of them because these values are not hard coded.

Here’s my textbox and my select element:

 <input type="text" name="text" id="text" style="position: absolute; top: 50px; left: 55px; width: 200px; height: 22px; right: 4px">

<select  id="inputSet1" multiple="multiple" Style="position: absolute; top: 93px; left: 55px; width: 200px; height: 200px; right: 4px; overflow-y: scroll">
    <option value="1">1</option> </select>

Can anyone help me to make my code filters the values I add. Thank you.

  • 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-16T03:29:22+00:00Added an answer on June 16, 2026 at 3:29 am

    It looks like the quick fix is to invoke filterByText after you have populated the option list.

    <%
     ArrayList<String> allGenes= new ArrayList<String>();
       allGenes.addAll(tree.getAllGenes()); 
    
     %>
    
      <%
       for(int i=0; i<allGenes.size(); i++)
       { %>
       var gene=document.createElement('option');
       var str="<%=allGenes.get(i)%>";
       gene.text= str;
       try
        {
            document.getElementById("inputSet1").add(gene,null);
        }
         catch(ex){
                  document.getElementById("inputSet1").add(gene);                             
         }
       <%}%>  
    $('inputSet1').filterByText($('input'));
    

    For the long term, I suggest you look into ajax. You would probably be better off passing data (in a format such as JSON) instead of writing java code that generates javascript code.

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

Sidebar

Related Questions

I am using Struts1.3, I have Jsp page which is showing the list of
I am making a web view using jsp, javascript and html. I have a
I have jsp using struts bean tag library: <html> <head> <meta http-equiv=Content-Type content=text/html; charset=UTF-8>
I have html in my db, would like it to be editable in a
I have parsed my HTML/JSP into DOM at compile time using JAVA. Now I
I have an HTML document that is generated on the server side using JSP.
I have an html/jsp form that I am serializing(using jquery) and passing to a
I have a jsp/html form that collects an array of choices from user. For
In a web project using jsp, I have following requirement Upload a file (say
My web application is written using jsp/javascripts. Backend Java. Have managed to implement the

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.