25

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. Kindly suggest me any direction so that I can acheive this,

Here is the following code hint which I am using

In HTML

<div id="web">
<object id="test"></object>
</div>

In JS

document.getElementById("web").innerHTML="<object id='test2'></object>";
.
.
var obj = document.getElementById("test2");

Here obj return null value.

3
  • 3
    Some code snippets would be helpful. Commented Mar 28, 2012 at 7:18
  • Try to use code snippet from [here][1] [1]: stackoverflow.com/questions/343145/… Commented Mar 28, 2012 at 7:21
  • Please check now, I have edited. Commented Mar 28, 2012 at 7:26

3 Answers 3

24

Did you assign an id to the freshly created element? Did you insert the element into the document tree (using appendChild or insertBefore)? As long as the element is not inserted into the DOM, you can't retrieve it using document.getElementById.

Example of element creation:

var myDiv = document.createElement('div');
myDiv.id = 'myDiv';
document.body.appendChild(myDiv);
document.getElementById('myDiv').innerHTML = 'this should have worked...';

[edit] Given the later supplied code, a third question emerges: is your script located at the bottom of your html page (right before the closing </body> tag)? If it's in the header of the document, your scripting may be running before the document tree is completely rendered. If your script has to be in the header of the document, you could use a load handler to run it after rendering of the document:

window.onload = function(){
  document.getElementById("web").innerHTML='<object id="test2"></object>';
  // [...]
  var obj = document.getElementById('test2');
};
Sign up to request clarification or add additional context in comments.

4 Comments

@kooilnc, where and how can I find the dynamically created element which has not been added to DOM?
@Phalgun in memory. In case of this answer you can address myDiv as long as it isn't added to the DOM. If it is added, the usual DOM-retrieval methods can be used to retrieve and/or manipulate it.
I have a scenario where I call a method exposed by third party(Angular File Saver - github.com/eligrey/FileSaver.js/blob/master/src/FileSaver.js) which creates an anchor element, forms createObjectURL() and clicks on it without adding to DOM. While trying to test this from protractor in AngularJS app, I am unable to find the dynamically created element. How would you suggest I get a reference to that anchor element created by Angular File Saver.
You're stressing my knowledge here. I know very little about Angular (and rather want to keep it that way ;). It may be an idea to create a new SO-question for this, perhaps referencing this one?
1

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

Comments

-4

If a DOM node is dynamically created, then there's no need EVER to find it again with document.getElementById().

By creating the node in the right way, you can keep a reference to it as a javascript variable, which can be used anywhere within scope.

For example:

window.onload = function(){
    var myDiv = document.createElement('div');
    document.body.appendChild(myDiv);

    //and now a function that's called in response to some future event
    function whatever(){
        ...
        myDiv.style.color = 'red';
        ...
    }
};

Note: The inner function (called at some point(s) future) has access to the myDiv variable because the outer function forms a "closure", within which variables are kept alive and accessible, even though the outer function has completed and returned.

2 Comments

hey @Beetroot-Beetroot,... I think this will be a trouble if an element newly created was created but we dont know the reference variable. How to achieve that getElementById() anyway? any 3-rd party injected or something....
This does not answer the question. It's better to not assume anything about their code base. That element creation could be in a part of the code base inaccessible to the part that grabs the element. Even if it is accessible, please consider others who come to this question looking for an answer to the question asked, not a workaround that works for this specific person.

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.