0

I am using MDC web loaded from CDN. Document for CDN users is better now when some years ago when I started. But I have problem with attachTo.

I build HTML elements with JavaScript and append them to the DOM. I got errors when applying attachTo before appending to the DOM. After testing (and searching for info) I decided to use a workaround, something like setTimeout(()=> ... apply attachTo here, 10).

Coming back to this some years later I wonder if there is any official way to do this now?

Leo
  • 4,136
  • 6
  • 48
  • 72
  • 1
    Is there a problem structuring your code so that appending to the DOM happens before `attachTo` call? – Konstantin Lyakh Feb 18 '22 at 16:23
  • @KonstantinLyakh Yes, I want to have attachTo together with the function that creates the element. Maybe it is instead possible to write a function that does `appendChild` and then `attachTo`. (I am not sure about the structure right now, I have to look at it to be sure.) However even if I do that the delay might be necessary. (Or a more technical variant using MutationObserver, but that feels like overkill.) – Leo Feb 18 '22 at 21:10
  • 1
    Very strange. As far as I understand, immediately after append call an element should be available within the DOM. And following call to attachTo should be able to find the element. Calling setTimeout releases single threaded JavaScript VM. But I think it should not be necessary for append call. If you can share your code it would be easier to understand the problem. – Konstantin Lyakh Feb 20 '22 at 15:13
  • @KonstantinLyakh Thanks, but I tried using `attachTo` before append. The reason is I wanted to keep `createElement` and `appendTo` within one function. (So I do not get errors that may be hard to find because I somewhere forgot the the `appendTo`.) – Leo Feb 21 '22 at 00:55

0 Answers0