0

I selected objects by class name

var inp = document.getElementsByClassName("tagsinput field is-grouped is-grouped-multiline input");

but in my file, there is only one element with that class, so I tried to console.log it

console.log(inp);

and it returned:

HTMLCollection []
length: 1
0: div.tagsinput.field.is-grouped.is-grouped-multiline.input
__proto__: HTMLCollection

then I tried to access it:

console.log(inp[0]);

but it returned:

undefined

I tried to run it in chrome devTools console and it worked just fine

Here is example code:

<script>
    var inp = document.getElementsByClassName("tagsinput field is-grouped is-grouped-multiline input");
    console.log(inp);
</script>

<script type="text/javascript"
    src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bulma-tagsinput.min.js">
    </script>
<script>
    bulmaTagsinput.attach();
</script>

3
  • 2
    Could you upload the HTML Element that you're trying to select? Commented Jan 2, 2020 at 23:26
  • 1
    Parse has a very specific definition for computing that doesn't apply here. You aren't parsing anything. Commented Jan 2, 2020 at 23:26
  • 1
    Probably this issue, but without more info, we can't say for sure (nor give a solution to the problem). Commented Jan 2, 2020 at 23:39

2 Answers 2

2

Looking at your added code example, it looks like the problem is a race condition. You're attempting to access the element before it's added to the DOM.

Adding a window.load event listener should fix the problem:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bulma-tagsinput.min.js"></script>
<script>
    window.addEventListener('load', (event) => {
        bulmaTagsinput.attach();
        var inp = document.getElementsByClassName("tagsinput field is-grouped is-grouped-multiline input");
        console.log(inp);
    });
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

That applies if you use that code directly in the console, but OP mentioned that it works fine from the DevTools console.
@Ivar the code example hadn't been posted before, which is why I assumed OP was confusing the output with the printed value.
-1

Here is the solution:

setTimeout(function () {
    console.log(inp[0]);
}, 1000);

Problem was that I was trying to parse an object that didn't exist.

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.