Forum Moderators: open

Message Too Old, No Replies

Possible to pack all this data into a div ?

         

PartisanEntity

2:39 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



I would like to do the following:

var content = '<img src="images/roma_1.jpg" alt="Rom" id="slideshow">';

document.getElementById('pictures') = content;

the html looks like this:

<div id="pictures"></div>

How would I got about doing this?

Trace

2:53 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



You're pretty close;

document.getElementById('pictures').innerHTML = content;

PartisanEntity

2:57 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



It's still not working, here is the error I get:

Error: document.getElementById("pictures") is null

rocknbil

3:14 pm on Aug 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Most likely because you're loading your JS at the top. Source code loads from the top down, so the div has not loaded yet, hence it's null. You can put your JS at the bottom, or use window.onload to attach the behavior. This allows you to put your JS in an external file.


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype on one line -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
window.onload=function() {
if (document.getElementById('pictures')) {
var content = '<img src="images/roma_1.jpg" alt="Rom" id="slideshow">';
document.getElementById('pictures').innerHTML=content;
}
};
</script>
</head>
<body>
<div id="pictures"> this is pics div</div>
</body>
</html>

PartisanEntity

6:56 pm on Aug 6, 2009 (gmt 0)

10+ Year Member



Thank you, that was it

dhtmlplus

10:56 am on Aug 9, 2009 (gmt 0)

10+ Year Member



I knew it, it was first tried when the page was not fully loaded.