[jQuery] Unbind not unbinding
<form name="newSpecificLeadSourceForm" action="#" method="post">
<input type="text" name="source" size="30" /></td>
<input type="text" name="code" />
<a href="#" id="suggestCode">suggest code</a>
<input type="submit" value="Save it!" />
</form>
...
var existingCodes = new Array('<?php echo implode("','",
$existingCodes); ?>');
var form = document.newSpecificLeadSourceForm;
$('#suggestCode').click(function() {
$(this).css('display', 'none');
$(form.source).bind('keyup', function() {
var code = $(this).attr('value').replace(/[^A-Z0-9\-]/ig, '');
if (existingCodes.indexOf(code) == -1)
$(form.code).attr('value', code);
}).keyup();
}).click();
$(form.code).focus(function() {
$(form.source).unbind('keyup');
$('#suggestCode').css('display', 'inline');
});
______
Intended behavior:
On every keypress within the "source" field, a striped-down version is
suggested within the "code" field. If the user clicks on the "code"
field, suggestion is turned off, and a "suggest code" link appears.
If the "suggest code" link is clicked, suggestion is turned back on,
and a code is suggested.
Currently, everything is working fine, except for the "suggestion is
turned off" part:
$(form.source).unbind('keyup');
I click into the "code" field (the "suggest code" link successfully
shows up), and enter a code. I then go to change the "source" field
*prior to pressing "suggest code"*, and it seems suggestion is still
on. I do not want suggestion to reappear until the user decides to
press "suggest code".
I've tried:
$(form.source).unbind();
$(form.source).unbind('keyup');
I eventually did a work around involving a "suggestionOn" boolean.
But using that method would be accepting defeat.
Any help? Am I using unbind incorrectly?