1

Edited to explain page loading

My HTML page has a div inside another div, both referenced by id, and both unique throughout the document.

<head>
<script type="text/javascript" src="myscript.js"></script>
</head>
...
<body onload=display()>
<div id="rightwork">
<div id="mychart" > </div>
</div>
...

My JavaScript is meant to write something within the 'mychart' div, but I can't manage to reference it :(

The question is related to this one except this one is a class inside an id.

This is my javascript:

function display() {
    var code = "<a onclick=\"second(); return false;\" href=\"#\">Hello</a>";
    document.getElementById('rightwork').innerHTML = code;
}

function second() {
    console.log(document.getElementById('rightwork'));
    console.log(document.getElementById('mychart'));
}

The line

document.getElementById("mychart");

returns null...

Whereas, this one works fine!

document.getElementById("rightwork");

This returns the expected div.

I attempted this

document.getElementById("rightwork").getElementById("mychart");

which of course does not work as getElementById("rightwork") returns a single element.

So, what's the solution to reference the inner div?

5
  • If document is loaded correctly why document.getElementById("mychart"); would not work? Commented Sep 4, 2015 at 10:00
  • Be aware that id must be unique as per-document. Commented Sep 4, 2015 at 10:07
  • @eomeroff you are right actually: I found the error: the HTML I display is generated by function display() and that one does not have getElementById... If I modify var code to: var code = "<div id=\"mychart\"><a onclick=\"second(); return false;\" href=\"#\">Hello</a></div>"; It will work. Yet, I wonder why rightwork is found in second() ? Commented Sep 4, 2015 at 10:43
  • 1
    @user1381 plase check in browser what html was rendered in #rightwork div. I bealive you are replacing #mychard div with content: <a onclick=\"second(); return false;\" href=\"#\">Hello</a> Commented Sep 4, 2015 at 13:50
  • @user1381 was my last comment helpful? Commented Sep 4, 2015 at 19:34

2 Answers 2

3

Just use

window.onload = function(){
   var myChart = document.getElementById("mychart");
   // ... code that loads chart
}

Or move

<script type="text/javascript" src="myscript.js"></script>

to the page footer to insure the dom is loaded before running the script

Sign up to request clarification or add additional context in comments.

1 Comment

You are right that this is a page loading issue, although your solution does not solve my particular case (edited). See my comment to eomeroff, I was missing the mychart id in the HTML code I was generating after a click.
1

Could you please post whole myscript.js content. If document is loaded correctly why document.getElementById("mychart"); would not work?

The thing might be that <div id="mychart" > </div> is not loaded while javascript is executed and <div id="rightwork"> was loaded in that time.

To ensure the document is fully loaded put your <script type="text/javascript" src="myscript.js"></script> at the end of html.

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.