1

I am using the jQuery plugin TagManager. I am having trouble dealing with empty arrays when the form gets initialized and I cant figure out why. Here is my script;

   $(".tm-input").each(function() {
      var e, l;
      e = $(this);
      l = e.data("load");
      e.tagsManager({
        prefilled: $.isEmptyObject(l) ? null : l
      });
    });

I set the data attribute data-load to the value I want to display.
The prefilled option is supposed to get set to null if l is empty and it's not working. If I break at the prefilled line above, the l variable value is Array[0]. If I check $.isEmptyObject(l) ? null : l it returns null, but the value that gets displayed is "[]". When the data-load value is null it works correctly.

Here is the affected form html;

 <input class="tm-input" data-load="[]" placeholder="enter tags here" value="[]" type="text"></input>  

Can someone help me figure out what's going on?

5
  • l is likely the string "[]". Commented Jul 1, 2014 at 18:45
  • If I set a break, it's value is Array[0]. Commented Jul 1, 2014 at 18:48
  • Either way, shouldn't the ternary line return null? Commented Jul 1, 2014 at 18:56
  • It returns null correctly, because $.isEmptyObject([]) == true Commented Jul 1, 2014 at 21:44
  • Yes, but the problem is, "[]" ends up as the value on the form. I probably wasn't clear on what the problem is. The problem is it ignores the "$.isEmptyObject(l) ? null : l" for setting the prefilled option and it gets set to "[]". All other values work correctly. Commented Jul 1, 2014 at 22:44

2 Answers 2

1

Following change may help you:

l = e.data("load") || 1 ;

if l is undefined, this line automatically assign value 1 to it. You can assign whatever value you want, based on your need.

Sign up to request clarification or add additional context in comments.

Comments

0

As it turns out, it's an issue with the way the TagsManager plugin handles empty arrays. It ignores the prefilled option and just displays "[]".

Here's what I did to work around it incase it can help someone else;

   $(".tm-input").each(function() {
      var e, l;
      e = $(this);
      l = e.data("load");
      if ($.isEmptyObject(l)) {
        l = null;
        e.val("");
      }
      e.tagsManager({
        prefilled: l
      });

This forces the input element value to an empty string if it's an empty array, which TagsManager handles better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.