How to pick up element by id within a block returned by getElementById()

Asked

Viewed 770 times

0

Following...

I want to find an element (form) that is inside a block. I use the .getElementById(); to get this block.

After I get the block, I want to find the form that’s in this block so I can include an element before this form. I’m trying this way, and so far, I haven’t been able:

javascript

var boardElem = document.getElementById('board-01');
var formEl = boardElem.children('#formNewTask');

It didn’t work... so I tried to use it like this too:

var formEl = boardElem.getElementById('formNewTask');

And the error generated is this:
Uncaught Typeerror: boardElem.getElementById is not a Function

I’m used to jQuery. But I’m developing a project without using the framework. Some light?

Thank you!

  • Do you have the form id at hand? It is known?

  • 1

    Another question: is there any other form on the page?

  • I just got it, Danilo! The id was "#formNewTask". There will be several forms on the page yes (I shouldn’t even try to pick up the ID). Anyway, thank you very much! :)

1 answer

1


I got...

I solved the problem. I needed a p/find selector element. I tried the children, getElementById, getElementByTagName, ...

And I wasn’t remembering the main... the querySelector.

It worked that way:

var boardElem = document.getElementById('board-01');
var formEl = boardElem.querySelector('form');
boardElem.insertBefore(novoElemento, formEl);

Like I’ll only have one form within the board, I will not use ID. It is the name of the tag as parameter in the selector. I was suffering from this!

  • 2

    That’s why I asked for more information in the comments, to better understand its structure =)

  • It is... End of business, the head starts to fail rsrsrs. Thank you, Danilo!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.