473,473 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Javascript Autocomplete with xmlHttp Function issue

3 New Member
Below is what I have build with several different languages. It works great but I need help, I am stuck.

When you click on an item in the dropdown autocomplete div it does a mousedown function and send the item number to the xmlHttp and works awesome. Now I need to add an apply button next to it. so they can type in the complete number then hit apply and it does the same function. I cant get it to work with the script I have. The image is apply where I am trying to get this to work.

you can see an example of this at http://www.wrighter.com/music/autocomplete.cfm

AutoComplete.prototype.onDivMouseDown = function() does the action to xmlHttp call and sending the div info.

I cant get it to take the info from the field and do the same thing. help!

[HTML]<!--- Autocomplete information from partList.dat in same folder
partList.dat is populated by query --->

<c:import url="partList.txt" var="importTXT"/>
<script language=javascript>

var objectTypes = ['Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.X MLHTTP'];

var xmlHttp;
if (window.XMLHttpRequest) {
//Browser is non-IE
xmlHttp = new XMLHttpRequest();
}
else {
for (o in objectTypes) {
try {
xmlHttp = new ActiveXObject(objectTypes[o]);
break;
} catch (ex) {//ignore exception
}
}
}

function AutoCompleteDB()
{
// set the initial values.
this.bEnd = false;
this.nCount = 0;
this.aStr = new Object;
}

AutoCompleteDB.prototype.add = function(str)
{
// increment the count value.
this.nCount++;

// if at the end of the string, flag this node as an end point.
if ( str == "" )
this.bEnd = true;
else
{
// otherwise, pull the first letter off the string
var letter = str.substring(0,1);
var rest = str.substring(1,str.length);

// and either create a child node for it or reuse an old one.
if ( !this.aStr[letter] ) this.aStr[letter] = new AutoCompleteDB();
this.aStr[letter].add(rest);
}
}

AutoCompleteDB.prototype.getCount = function(str, bExact)
{
// if end of search string, return number
if ( str == "" )
if ( this.bEnd && bExact && (this.nCount == 1) ) return 0;
else return this.nCount;

// otherwise, pull the first letter off the string
var letter = str.substring(0,1);
var rest = str.substring(1,str.length);

// and look for case-insensitive matches
var nCount = 0;

if (letter >= 0 && letter <= 9 )
{
var lNumber = letter;
if ( this.aStr[lNumber] )
nCount += this.aStr[lNumber].getCount(rest, bExact && (letter == lNumber));

}
else
{
var lLetter = letter.toLowerCase();
if ( this.aStr[lLetter] )
nCount += this.aStr[lLetter].getCount(rest, bExact && (letter == lLetter));

var uLetter = letter.toUpperCase();
if ( this.aStr[uLetter] )
nCount += this.aStr[uLetter].getCount(rest, bExact && (letter == uLetter));
}

return nCount;
}

AutoCompleteDB.prototype.getStrings = function(str1, str2, outStr)
{
if ( str1 == "" )
{
// add matching strings to the array
if ( this.bEnd )
outStr.push(str2);

// get strings for each child node
for ( var i in this.aStr )
this.aStr[i].getStrings(str1, str2 + i, outStr);
}
else
{
// pull the first letter off the string
var letter = str1.substring(0,1);
var rest = str1.substring(1,str1.length);

if (letter >= 0 && letter <= 9 )
{
var lNumber = letter;
if ( this.aStr[lNumber] )
this.aStr[lNumber].getStrings(rest, str2 + lNumber, outStr);

}
else {

// and get the case-insensitive matches.
var lLetter = letter.toLowerCase();
if ( this.aStr[lLetter] )
this.aStr[lLetter].getStrings(rest, str2 + lLetter, outStr);

var uLetter = letter.toUpperCase();
if ( this.aStr[uLetter] )
this.aStr[uLetter].getStrings(rest, str2 + uLetter, outStr);
}
}
}


function AutoComplete(aStr, oText, oDiv, nMaxSize)
{
// initialize member variables
this.oText = oText;
this.oDiv = oDiv;
this.nMaxSize = nMaxSize;

// preprocess the texts for fast access
this.db = new AutoCompleteDB();
var i, n = aStr.length;
for ( i = 0; i < n; i++ )
{
this.db.add(aStr[i]);
}

// attach handlers to the text-box
oText.AutoComplete = this;
oText.onkeyup = AutoComplete.prototype.onTextChange;
oText.onblur = AutoComplete.prototype.onTextBlur;
}


AutoComplete.prototype.onTextBlur = function()
{
this.AutoComplete.onblur();
}

AutoComplete.prototype.onblur = function()
{
this.oDiv.style.visibility = "hidden";
}
AutoComplete.prototype.onblur = function()
{
this.oDiv.style.visibility = "hidden";
}

AutoComplete.prototype.onTextChange = function()
{
this.AutoComplete.onchange();
}

AutoComplete.prototype.onDivMouseDown = function()
{
this.AutoComplete.oText.value = this.innerHTML;
var url="MWQuickOrderForm.jsp?theText=" + document.GoForm.theText.value;
xmlHttp.open('get', url, true);
xmlHttp.onreadystatechange = myResponseMethod;
xmlHttp.send(null);
}
function myResponseMethod() {
//readyState 4 means complete
if(xmlHttp.readyState == 4) {
if (xmlHttp.status == 200 || xmlHttp.status === 0) {
var char_id="xmlhttp_id"; //characteristic identifier of the wrapper
if (document.getElementById(char_id)) {
var obj=document.getElementById(char_id);
obj.parentNode.removeChild(obj);
}
var odiv=document.createElement("div")
odiv.id=char_id; //to prepare for removal for repetitive calls
odiv.innerHTML=xmlHttp.responseText;
document.getElementById("theItem").appendChild(odi v);
document.getElementById("theItem").style.visibilit y = "visible";
document.getElementById("theText").style.display = "none";
document.getElementById("theDiv").style.display = "none";
document.getElementById("theHeader").style.display = "none";

}

}
}
<!---Add Highlight Here --->
AutoComplete.prototype.onDivMouseOver = function()
{
this.className = "AutoCompleteHighlight";
this.className = this.style.backgroundColor='Yellow';
}

AutoComplete.prototype.onDivMouseOut = function()
{
this.className = "AutoCompleteBackground";
this.className = this.style.backgroundColor='White';
}

AutoComplete.prototype.onchange = function()
{
var txt = this.oText.value;

// count the number of strings that match the text-box value
var nCount = this.db.getCount(txt, true);

// if a suitable number then show the popup-div
if ( (this.nMaxSize == -1 ) || (nCount > 0) ) // max size -((nCount < this.nMaxSize) ) &&
{
// clear the popup-div.
while ( this.oDiv.hasChildNodes() )
this.oDiv.removeChild(this.oDiv.firstChild);

// get all the matching strings from the AutoCompleteDB
var aStr = new Array();
this.db.getStrings(txt, "", aStr);

// add each string to the popup-div

if (aStr.length < 25)
{ var i, n = aStr.length; }
else
{ var i, n = 26 }

for ( i = 0; i < n; i++ )
{
var oDiv = document.createElement('div');
this.oDiv.appendChild(oDiv);
oDiv.innerHTML = aStr[i];
oDiv.onmousedown = AutoComplete.prototype.onDivMouseDown;
oDiv.onmouseover = AutoComplete.prototype.onDivMouseOver;
oDiv.onmouseout = AutoComplete.prototype.onDivMouseOut;
oDiv.onblur = AutoComplete.prototype.onTextBlur;
oDiv.AutoComplete = this;
}
this.oDiv.style.visibility = "visible";
}
else // hide the popup-div
{
this.oDiv.innerHTML = "";
this.oDiv.style.visibility = "hidden";
}
}

function createAutoComplete()
{
var words = new String("<c:out value="${importTXT}"/>")
var swords = words.split(";")
var aNames = swords ;
new AutoComplete(
aNames,
document.getElementById('theText'),
document.getElementById('theDiv'),
35
);
}
</script>
<body onload="document.getElementById('theText').focus() ; createAutoComplete();">

<form name="GoForm">
<%
String theText = null;
if (request.getParameter("theText") == null) {
%>
<div id="theHeader">
<table>
<tr><td colspan="2">- Start Typing the Product Number -</td></tr>
<tr><td><input vspace="6" name="theText" id="theText" type="text" autocomplete="off"></td><td><a href="" onClick="AutoComplete.prototype.onDivMouseDown();" ><img src="<c:out value="${imagesPath}"/>/btn/btn_apply.gif" alt="Apply" border="0" /></a></td></tr>
</table>
</div>
<div id="theDiv" style="width:150px; padding-right:4px;padding-left:4px;visibility:hidden;border:solid green 2px;background-color:white;z-index:1">
</div>
<%
}
%>
</form>

<div id="theItem" class="borleftblue">

</div>[/HTML]
Mar 19 '07 #1
1 3819
acoder
16,027 Recognized Expert Moderator MVP
Take the anonymous function, declare it as a function in its own right and call that instead.
Jun 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: sneill | last post by:
Using XMLHTTP and DOM I'm able to load new HTML page content. I'd now like to load small snippets of javascript with the HTML markup and have that <script> incorporated into the page. If any of...
3
by: Dav | last post by:
Hi, I moved over client side javascript code from an asp.net 1.1 project to an asp.net 2.0 project. When I view the web page, I get all these "is null or is not object" javascript errors. Is...
4
by: yllar2005 | last post by:
Hi! I'm using Msxml2.ServerXMLHTTP.3.0 to fetch a HTML page on a remote server. The fetched page is then parsed and the information of interest is extracted and send to the client browser. ...
1
by: korggrok | last post by:
OK, this makes no sense to me, perhaps someone understands what's happening. Developing a CGI app. A page contains a table with <th> header elements and <tdtable elements, both if which contain...
4
by: Adnan Siddiqi | last post by:
Hi I am using Walter Zorne DragnDrop lib along with graphic library for my application. the function Set_DHTML() in drag n drop library sets divs(s) for dragging purpose. the argument of...
2
by: WGW | last post by:
Hello all, I need another set of eyes cause it just isn't working, no matter how identical I make it. The initRotator function works when called by itself, but when adding another function, only the...
1
by: Ivann | last post by:
Hi All, I have a javascript/css menut that i have which i start when the html page loads. <script type="text/javascript">cssdropdown.startchrome("chromemenu")</ script> ***THIS IS IN THE...
2
by: trpost | last post by:
Is it possible to execute javascript as passed in xmlHttp.responseText Here is what I am doing: search.js var xmlHttp xmlHttp=GetXmlHttpObject() var url="search.php"...
1
mageswar005
by: mageswar005 | last post by:
Hello, In My Application i have open the PDF file from javascript open.window function.In This scenario i want to control / Hide the PDF Toolbar from open.window function. My Current code is...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.