By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

Probably A Simple Answer...Text Display...

P: n/a
I can't get this simple crappp!!

Not understanding why my text isn't displaying....

<html><head><title>Problem6</title>

<script type="text/javascript">

var my_message="I love football";
var my_div=document.getElementById("space");
my_div.innerHTML(my_message);

</script>
<style type="text/css">

#space{position:absolute; left:10px; top:20px;}

#mybuttons{position:absolute; left:10px; top:75px;}

</style></head>
<body>
<div id="space">
</div>
<div id = "mybuttons">
<input type="button" value="startscroll" id="start">
<input type="button" value="stopscroll" id="stop">
</div>

</body>
</html>

--
Message posted via http://www.webmasterkb.com

Sep 28 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
my_div.innerHTML = my_message;

Tim

"ChuckB via WebmasterKB.com" <u46201@uwewrote in message
news:8ad8274b0aeae@uwe...
>I can't get this simple crappp!!

Not understanding why my text isn't displaying....

<html><head><title>Problem6</title>

<script type="text/javascript">

var my_message="I love football";
var my_div=document.getElementById("space");
my_div.innerHTML(my_message);

</script>
<style type="text/css">

#space{position:absolute; left:10px; top:20px;}

#mybuttons{position:absolute; left:10px; top:75px;}

</style></head>
<body>
<div id="space">
</div>
<div id = "mybuttons">
<input type="button" value="startscroll" id="start">
<input type="button" value="stopscroll" id="stop">
</div>

</body>
</html>

--
Message posted via http://www.webmasterkb.com

Sep 28 '08 #2

P: n/a
On 2008-09-28 03:31, Tim Williams wrote:
my_div.innerHTML = my_message;
Are you sure?
There is no element with the id "space" at this point...
- Conrad
Sep 28 '08 #3

P: n/a
Tim Williams wrote:
>my_div.innerHTML = my_message;

Tim
>>I can't get this simple crappp!!
[quoted text clipped - 26 lines]
></body>
</html>
Thanks for the response...tried that...still nothing:

<html><head><title>Problem6</title>

<script type="text/javascript">

var my_message="I love football";
var my_div=document.getElementById("space");
my_div.innerHTML=my_message;

</script>
<style type="text/css">

#space{position:absolute; left:10px; top:20px; width:200px; height:50px;
border:1px solid black;}

#mybuttons{position:absolute; left:10px; top:75px;}

</style></head>
<body>
<div id="space">
</div>
<div id = "mybuttons">
<input type="button" value="startscroll" id="start">
<input type="button" value="stopscroll" id="stop">
</div>

</body>
</html>

--
Message posted via http://www.webmasterkb.com

Sep 28 '08 #4

P: n/a
Conrad Lender wrote:
>my_div.innerHTML = my_message;

Are you sure?
There is no element with the id "space" at this point...

- Conrad
There's the div element....

--
Message posted via http://www.webmasterkb.com

Sep 28 '08 #5

P: n/a
On 2008-09-28 03:38, ChuckB via WebmasterKB.com wrote:
>>There is no element with the id "space" at this point...

There's the div element....
Where?
13 lines after you try to access it?

The element has to exist before you can "get it by ID".
- Conrad
Sep 28 '08 #6

P: n/a
Conrad Lender wrote:
>>>There is no element with the id "space" at this point...

There's the div element....

Where?
13 lines after you try to access it?

The element has to exist before you can "get it by ID".

- Conrad
Right here:

<body>
...
<div id="space">
</div>
...
</body>

Am I missing something here?

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200809/1

Sep 28 '08 #7

P: n/a
ChuckB via WebmasterKB.com:
Conrad Lender wrote:
>>>>There is no element with the id "space" at this point...

There's the div element....

Where?
13 lines after you try to access it?

The element has to exist before you can "get it by ID".

- Conrad

Right here:

<body>
..
<div id="space">
</div>
..
</body>

Am I missing something here?
Your JavaScript is running before the DIV exists. You'll want to wrap your
code into a function and call it once the page has finished loaded. At that
point, the DIV will exist.

<script type="text/javascript">
function doOnload() {
var my_message="I love football";
var my_div=document.getElementById("space");
my_div.innerHTML(my_message);
}
</script>

Then you can call this new "doOnload" function when the body has loaded:

<body onload="doOnload()">

Sep 28 '08 #8

P: n/a
On 2008-09-28 04:28, Ben Amada wrote:
Your JavaScript is running before the DIV exists. You'll want to wrap your
code into a function and call it once the page has finished loaded. At that
point, the DIV will exist.
Exactly. But don't forget to fix the other error that Tim pointed out:
function doOnload() {
var my_message="I love football";
var my_div=document.getElementById("space");
my_div.innerHTML(my_message);
my_div.innerHTML = my_message;

Strictly speaking, innerHTML isn't even necessary here (and it's a
proprietary property). "mydiv.firstChild.data = my_message" will do just
fine in this simple case.
- Conrad
Sep 28 '08 #9

P: n/a
Conrad Lender wrote:
>Your JavaScript is running before the DIV exists. You'll want to wrap your
code into a function and call it once the page has finished loaded. At that
point, the DIV will exist.

Exactly. But don't forget to fix the other error that Tim pointed out:
>function doOnload() {
var my_message="I love football";
var my_div=document.getElementById("space");
my_div.innerHTML(my_message);

my_div.innerHTML = my_message;

Strictly speaking, innerHTML isn't even necessary here (and it's a
proprietary property). "mydiv.firstChild.data = my_message" will do just
fine in this simple case.

- Conrad
Got it...Lesson learned...always call JavaScript code with a function or else
it will execute before the html appears.

I think that's the lesson....

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200809/1

Sep 28 '08 #10

P: n/a
On 2008-09-28 04:45, ChuckB via WebmasterKB.com wrote:
Got it...Lesson learned...always call JavaScript code with a function or else
it will execute before the html appears.

I think that's the lesson....
Almost, but not quite. Take this case:

<script type="text/javascript">
function insertMyText () {
var my_div = document.getElementById("space");
my_div.innerHTML = "I love football";
}
insertMyText();
</script>
<div id="space"</div>

That will still give you an error, even though your code was executed in
a function. This however will work fine:

<div id="space"</div>
<script type="text/javascript">
var my_div = document.getElementById("space");
my_div.innerHTML = "I love football";
</script>

The point is that you can only access elements (by ID or otherwise)
after they have been "seen" by the scripting engine (which, like us,
reads the document from top to bottom). To be absolutely sure that all
elements are ready, you can wait until the "load" event. That's what Ben
did when he called his function from the body element's "onload" attribute.
- Conrad
Sep 28 '08 #11

P: n/a
Conrad Lender wrote:
>Got it...Lesson learned...always call JavaScript code with a function or else
it will execute before the html appears.

I think that's the lesson....

Almost, but not quite. Take this case:

<script type="text/javascript">
function insertMyText () {
var my_div = document.getElementById("space");
my_div.innerHTML = "I love football";
}
insertMyText();
</script>
<div id="space"</div>

That will still give you an error, even though your code was executed in
a function. This however will work fine:

<div id="space"</div>
<script type="text/javascript">
var my_div = document.getElementById("space");
my_div.innerHTML = "I love football";
</script>

The point is that you can only access elements (by ID or otherwise)
after they have been "seen" by the scripting engine (which, like us,
reads the document from top to bottom). To be absolutely sure that all
elements are ready, you can wait until the "load" event. That's what Ben
did when he called his function from the body element's "onload" attribute.

- Conrad
Okay, got it...now if you don't mind...please respond to the REAL problem I'm
having on the NEW POST. ..now that I finally got that out the way :-)

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200809/1

Sep 28 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.