Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

word filtering problem

Status
Not open for further replies.

programgirl

Programmer
Sep 11, 2005
4
US
I have written javascript code helps me filter keywords options in a <html:select> tag, in a JSP page.
(The reason I'm posting here is because my problem is with my javascript code.

so, in my JSP page I have the following as my textarea:

Code:
<html:text property="metrics" style="query-table-item" size="40" styleId="metricsTxt" onchange="doSelection(this);" />

and the following is my javascript code, for doSelection, which helps me filter out words as I type them. The problem, however, is that after I erase a keyword, all my options will get selected - when I want them all deselected.
Also, when I'm entering keywords, I have to press the tab key to see the changes.
Is there a way I can see the change in the <html:select> tag as I go along? without having to enter any keys?

Code:
function doSelection (control)
{
    // control is the textbox control
   var typedText = control.value;
   var selectControl = document.getElementById("metricsSelect");
   
   //when keyword starts with the typed text
   if(typedText==null)
   {
	for (var i=0; i<selectControl.options.length; i++)
	{
		if(selectControl.options[i].selected == true)
		{
			selectControl.options[i].selected = false;
		}
	}	
   }
   else
   {
   	if(document.getElementById("keywordSelect").checked)
   	{
		for (var i=0; i<selectControl.options.length; i++)
		{
			if(selectControl.options[i].text.indexOf(typedText)==0)
			{
				selectControl.options[i].selected= true;
	 		}
      			else
         		{
         				selectControl.options[i].selected = false;  // unselects non-matches
	 		}
		
		}
   	}
   	//when keyword contains the typed text
   	if(document.getElementById("keywordSelect2").checked)
   	{
   	
		for (var i=0; i<selectControl.options.length; i++)
   		{
			//if (typedText!=null && typedText==selectControl.options[i].text.substring(0,typedText.length+1))
	 		if(selectControl.options[i].text.indexOf(typedText)>=0)
         		{
				selectControl.options[i].selected= true;
	 		}
      			else
         		{
         			selectControl.options[i].selected = false;  // unselects non-matches
	 		}
   		} 
   	}
   	//when keyword ends with the typed text
   	if(document.getElementById("keywordSelect3").checked)
   	{
	
		for (var i=0; i<selectControl.options.length; i++)
		{
			var pos = selectControl.options[i].text.indexOf(typedText);
			if(selectControl.options[i].text.substring(pos)==typedText)
			{
				selectControl.options[i].selected= true;
	 		}
      			else
         		{
         			selectControl.options[i].selected = false;  // unselects non-matches
	 		}
		}
   	}
   }
   

}
 
Also, when I'm entering keywords, I have to press the tab key to see the changes.
Is there a way I can see the change in the <html:select> tag as I go along? without having to enter any keys?
You would change to using the onKeyUp event instead of the onchange (which would necessitate a bit of a extra code, but not much).

Regards the rest of your problem... I don't quite understand what you envisage happening. Maybe you could reply with the english language variation -- since I'm sure you have a minor logic bug in your code and understanding what you want to do in plain english terms will help identify that bug.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top