getElementById Where Element is dynamically created at runtime

后端 未结 3 766
一向
一向 2020-11-30 08:33

I have created an object at runtime by using innerHTML tag, now I want to access this element by using getElementById, when I accessed the element its return NULL value. Kin

3条回答
  •  伪装坚强ぢ
    2020-11-30 09:18

    To add an element using JavaScript, you need to do 2 things.

    1. Create the element

      var element = document.createElement(tagName);

    2. Add it to the dom

      document.body.insertBefore(selector, element);

    or

      document.getElementByID(selector).appendChild(element);
    

    More info here: MDN

提交回复
热议问题