473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to use innerHTML

Hello all,

I have a few questions about the following code:

function load() {
document.getElementById('sidebar').innerHTML = "<a
href='javascript:test()'>Hello</a>";
}
function test() {
document.getElementById('main').innerHTML = "World";
}
<body onload='load()'>
<div id='main' />
<div id='sidebar' />
</body>

When I go to the page, it displays the "Hello" link right away (as it
should). However, when I click on the link, "Hello" goes away and
"World" appears. Why is this? I expected "Hello" to stay on the page
and "World" to appear on the page since they are in different div's.
Why does inserting HTML into one div remove the HTML from other divs?

One more question... before, I had <div id='main' /after <div
id='sidebar' /in the <body>. However, when I had it this way I would
get an error saying "document.getElementById('main') has no
properties". Why does this happen when main is after sidebar, but not
when it's before?

Oct 15 '06 #1
1 5092
On Oct 15, 1:00 am, "Bryan" <BTRichard...@gmail.comwrote:
Hello all,

I have a few questions about the following code:

function load() {
document.getElementById('sidebar').innerHTML = "<a
href='javascript:test()'>Hello</a>";}function test() {
document.getElementById('main').innerHTML = "World";}<body onload='load()'>
<div id='main' />
<div id='sidebar' />
</body>

When I go to the page, it displays the "Hello" link right away (as it
should). However, when I click on the link, "Hello" goes away and
"World" appears. Why is this? I expected "Hello" to stay on the page
and "World" to appear on the page since they are in different div's.
Why does inserting HTML into one div remove the HTML from other divs?
Because you are using xHTML syntax in IE and relying on error
correction which IE "error corrects" the above HTML to this:

<div id="main"></div>

Change your HTML to this:

<div id='main'></div>
<div id='sidebar'></div>

And ti will work the way you wanted it too. Also, read the group FAQ
with regards to javascript: in the HREF of a link.
One more question... before, I had <div id='main' /after <div
id='sidebar' /in the <body>. However, when I had it this way I would
get an error saying "document.getElementById('main') has no
properties". Why does this happen when main is after sidebar, but not
when it's before?
See above.

--
Randy

Oct 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris | last post by:
How can I get the innerHTML of a <div> area only when the page loads, then use that variable in a function? Here is my code: function setContent(zz) { var lb =...
7
by: KK | last post by:
Please help! I am currently experiencing a bug in Safari v125.9. When I modify the value of form input box and then get the innerHTML property of the surrounding div object - I am returned the...
6
by: Andrew Poulos | last post by:
Given that I need to be able to add a TYPE attribute when I'm using createElement and it seems to fail in both IE and FF (but not MZ) is it 'safer' to use innerHTML instead? I can dynamically...
4
by: RobG | last post by:
I know you aren't supposed to use innerHTML to mess with table structure, but it seems you can't use it just after a table without damaging the containing element. I added a table to a div using...
9
by: Hallvard B Furuseth | last post by:
Why does the FAQ (Q 4.15) recommend innerHTML when so many here say one should use createElement(), replaceChild() etc? Also, why does the "Alternative DynWrite function" at...
2
by: sveinn | last post by:
Hi all, I've read through this group searching for an answear about this problem. Few have come close but not quite what I need. My problem is this: I'm using Ajax to fetch a new table with...
17
by: PJ | last post by:
Greetings... I have stumbled upon a small problem. I use Ajax to retrieve part of a page I need to update. I update a DIV element with the HTML contents I get from another page. It works...
9
by: martymix | last post by:
simple question: I have a simple <dt>test text</dt> I get the innerHTML of that dt, and I try and append some text to it like so: dt = document.getElementsByTagName('dt') var text =...
6
by: PaPa | last post by:
I'm not sure this is a javascript issue or an HTML issue. I notice that when I extract the contents of a div using the innerHTML property (?), that I wind up with a literal variable (?) which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.