Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silent error when first argument is jQuery object #23

Closed
jcubic opened this issue May 29, 2022 · 4 comments · Fixed by #40
Closed

Silent error when first argument is jQuery object #23

jcubic opened this issue May 29, 2022 · 4 comments · Fixed by #40
Labels
bug Something isn't working

Comments

@jcubic
Copy link
Owner

jcubic commented May 29, 2022

If you pass jQuery object as first argument component is created but the return object is not tagger object. Which in turn throws different error when you try to invoke a method.

@jcubic
Copy link
Owner Author

jcubic commented May 29, 2022

The issue is that tagger thinks that jQuery object is an array and doesn't return tagger object but an array.

        if (input.length) { 
             return Array.from(input).map(function(input) { 
                 return new tagger(input, options); 
             }); 
         }

@jcubic jcubic added the bug Something isn't working label Aug 23, 2022
@lucasnetau
Copy link
Contributor

This also occurs if you initialise using document.querySelectorAll which will return a NodeList even if only a single element exists.

Adding this to the top of the function tagger(input, options) will flatten the array if it contains only one element, works for both $() and document.querySelectorAll()

if (input.length && input.length === 1) {
    input = Array.from(input).pop();
}

@jcubic
Copy link
Owner Author

jcubic commented Sep 20, 2022

@lucasnetau do you want to create a PR with this fix?

@lucasnetau
Copy link
Contributor

@jcubic sure I'll make one. I'll also add some documentation around if a single instance is created it will always be returned as an object even if it was provided in a single element array.

Single Instance initialised => tagger() returns object
Multiple instances initialised => tagger() returns object[]

lucasnetau added a commit to lucasnetau/tagger that referenced this issue Jul 21, 2023
 * Return without creation if an empty NodeList or iterable is passed in
 * Flatten a NodeList or iterable if it contains a single element
 * Document the return type of tagger() in the readme to cover the single element in list case

 Closes jcubic#23
lucasnetau added a commit to lucasnetau/tagger that referenced this issue Jul 21, 2023
…odeList or iterable is passed in * Flatten a NodeList or iterable if it contains a single element * Document the return type of tagger() in the readme to cover the single element in list case

 Closes jcubic#23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants