Gazler
Gazler

Reputation: 84140

jquery autocomplete input array

I have the following code and what I am trying to do is set up an Autocomplete input box that when it is filled out, create another autocomplete input box underneath it.

Code:

function setupAutoComplete()
{
 var autoCompleteData = $("#listContacts").html().split("<br>");
 //autoCompleteData = replaceAll(autoCompleteData, "&lt;", "<");
 //autoCompleteData = replaceAll(autoCompleteData, "&gt;", ">");
 $("[name|=toemail[]]").autocomplete(autoCompleteData);
 $("[name|=toemail[]]").result(function(event, item) {
    $("[name|=toemail[]]").blur();
 });
 $("[name|=toemail[]]").blur(function(){
  var newString = $(this).val();
  newString = replaceAll(newString, "&lt;", "<");
  newString = replaceAll(newString, "&gt;", ">"); 
  $(this).val(newString);
  var newfield = '<p><label class="" disabled="true"><select name="toSelect[]"><option>To: </option><option>CC: </option><option>BCC: </option></select></label><input type="text" value="" name="toemail[]" /></p>';
  $("#composeTo").append(newfield);
 });
}

Upvotes: 0

Views: 2075

Answers (2)

Gazler
Gazler

Reputation: 84140

Posting my own solution.

function setupAutoComplete()
{
    var autoCompleteData = $("#listContacts").html().split("<br>");


$("[name|=toemail[]]").each(function(key, value) {
    if (!($(this).hasClass("blurred")))
    {
    $(this).autocomplete(autoCompleteData);
    $(this).result(function(event, item) {
        var newString = $(this).val();
        newString = replaceAll(newString, "&lt;", "<");
        newString = replaceAll(newString, "&gt;", ">"); 
        $(this).val(newString);
        $(this).blur();
    });
    $(this).blur(function(){
        if (!($(this).hasClass("blurred")))
        {
        var newfield = '<p><label class="" disabled="true"><select name="toSelect[]"><option>To: </option><option>CC: </option><option>BCC: </option></select></label><input type="text" value="" name="toemail[]" /></p>';
        $("#composeTo").append(newfield);
        setupAutoComplete();
        $(this).addClass("blurred");
        }
    });
    }
});

Upvotes: 0

mat-mcloughlin
mat-mcloughlin

Reputation: 6702

Rather than setting up the auto complete on the elements as a group you should be doing it individually as each one is created. This will stop the recursion.

Secondly to avoid the onblur event firing when you click somewhere else on the screen you should catch the tab key being pressed on a keypress.

Upvotes: 1

Related Questions