Link to home
Start Free TrialLog in
Avatar of ericdalrymple
ericdalrymple

asked on

jquery autocomplete works initially but fails after redraw

I uploaded an image to help explain... called autocompleteproblem.gif but not sure where that shows up...

I am using jorn version of jquery autocomplete and have a problem.
I have a page that has a div containing a form (and my auto complete field). Initially, this div is populated when the page is drawn, however, the user can trigger an ajax request that, upon returning, refills the contents of the div with updated info.
On The initial load, my autocomplete field works great. However, after the div contents are reloaded via ajax... nothing happens when i type in my auto complete field.

I placed an alert() into autocomplete.js in the function onChange(crap, skipPrevCheck) {. On initial load, when i type in my field, i get the alert. however, the alert does not fire the second time.

something to note... in my ajax callback, when I am refilling the contents of the div via ajax, I am using innerHTML to set the contents... I have to escape quotes etc.
e.g. mydiv.innerHTML="the entire form including my autocomplete field"

is there something to do with the dom ... or CSS... where a pointer to my autocomplete field is created initially... then when the new version of the div tries to do it again... there is a conflict?
Obviously I am very new to this stuff...
$("#item_num").autocomplete(
    "http://myurl.com&",
     {
    delay:400,
    minChars:2,
    matchSubset:1,
    maxItemsToShow:12,
    matchContains:1,
    extraParams:{mytype:function() {return formPartsUsed.request_type[0].checked;},itemnum:function() {return formPartsUsed.item_num.value;}},
    hideCallback : function() {document.getElementById("pleasewait").style.visibility="hidden";},
    cacheLength:0,
    onItemSelect:selectItem,
    autoFill:false
  }
);
 
 
Here is a simplified example: (Oracle Pl/SQL)
----INITIAL PAGE LOAD---
<div id=mydiv>
<form name=form1 method=post action="ajax post">
<input type=text class="ac_input" alt="Item#" name="item_num" id="item_num">
<input type=button onclick="form1.submit();">
... more div content
</form>
</div>
 
---AJAX REQUEST CALLBACK (replaces the div with new results.)---
strContent := '<form name=form1 method=post action="ajax post">';
strContent := strContent || '<input type=text class="ac_input" alt="Item#" name="item_num" id="item_num">';
strContent := strContent || '<input type=button onclick="form1.submit();">';
strContent := strContent || '... more div content';
document.getElementById("mydiv").innerHTML=REPLACE(strContent,'"','\"');

Open in new window

Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Not sure you want to submit that form - it could unload the page

Why not call the ajax submit from the button instead?
Avatar of ericdalrymple
ericdalrymple

ASKER

The page has tabs. I have a single div that holds the contents of a tab. When user clicks on a different tab I use Ajax to get the contents for that tab and fill the div... Not really doing a form submit... Using Ajax to retrieve contents of div.
When the page draws initially, my div that contains the autocomplete field works fine. When I grab the exact same contents and fill the div(user clicked a different tab then returns to original tab) it stops working.
trying this attach file functionality again... dont see where it can be viewed from the first time i did it.
autocompleteproblem.jpg
This sure looks like a submit to me
onclick="form1.submit();"
i just put the button with submit as an example... sorry... when the user changes tabs it fires off the ajax below. When it comes back it makes the div visible and innerHTML the contents of the tab.

anyway... the problem is that after this is done... the autocomplete stops working.

thanks for your reply...
function getPartsRemoved(){
//hide the div
document.getElementById("bottomTabDiv").style.visibility="hidden";
var sFrmData = "";
var sURL="myurl";
doAjax(sURL,sFrmData);
}
 
function doAjax(sURL,sFrmData){';
$.ajax({
    type: "POST",
    url: sURL,
    data: sFrmData,
    success: function(ajaxResponse){
    eval(ajaxResponse);
  }
 });	
}		

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ericdalrymple
ericdalrymple

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial