I have this ajax call, which works for the most part, but it does seem to memorize the previous result. Edit: this likely is due to binding a behavior to a divid.
The structure of my routine is like this:
-
call ajax to search if the input is good or not, get response back from php. Result can be either Yes or No.
-
If result is Yes, change the color of a holder div as a visual cue, allow user to doubleclick on the div to run another function. If not, don’t allow doubleclick div and don’t run another function.
Easy right? Except that when the first ajax call yields a ‘Yes’ result, and then the user perform a second ajax call and it yields a ‘No’ result, the user can dblclick on ‘No’ result and perform the action intended for a ‘Yes’ result.
I try to unbind the dblclick, but it does not seem to work, and would still allow the no result to be clicked on. This is my jquery code, my regular javascript code has the same bad behavior.
Edit: this appears to be a binding problem. But I can seem to unbind and destroy/delete the previously bound behavior. Further testing by repeating the routine seem to indicate that javascript does save these bound objects in memory because the alert would echo back as many “saved” bound objects when I repeated the error routine many times.
My partial code is below, any insight is appreciated.
TIA
Edit: the solution is to unbind the doubleclick, I should had type dblclick instead of doubleclick
function search(input,divid) {
var queryString = "name=" + input+"&divid="+divid;
var url = "../l.php";
$.ajax({
type: 'get',
url: url,
data: queryString,
cache: false,
dataType: 'text',
timeout: 10000,
error: function(xhr, status, error) { alert('Search Error: '+ xhr.status+ ' - '+ error); },
success: function(response) {
var result = response;
var err = /:/;
var divid = "#"+divid;
if (result == "Yes" )
{
$(tldid).attr({'class':'tg1', 'title':'Doubleclick to save'});
var testword = 'xyz';
$(divid).dblclick(function() {
alert (testword);
$(divid).unbind('doubleclick');
});
else {
//alert (testword +" should not have been passed here, but it does");
$(divid).attr({'class':'tg2'});
}
}//success
});
}//end
You should change
$(divid).unbind('doubleclick');to$(divid).unbind('dblclick');.