473,414 Members | 1,596 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,414 software developers and data experts.

innerHTML query

Hi,

I am trying to write code that will insert HTML into an empty <span>
tag. The function that will do this is add_item(), which works OK
where the code is inserted using the innerHTML command/method. The
problem occurs when i want to overwrite the code using a similar
function. it doesnt seem to do anything let alone overwrite. below is
a stripped down version of what i am tryin to achieve:

<html>
<head>
<script>

function add_item() {

item1.innerHTML = '<p>Insertion</p>';

}

function delete_item() {

item1.innerHTML = '<p>Insertion now overwritten</p>';

}

</script>
</head>
<body>
<span onClick="add_item()">Click here to insert</span><br><br>
<span onClick="delete_item()">Click here to delete</span><br><br>
<span id="item1"></span>
</body>
</html>
if anybody could tell me why it doesnt overwrite or another approach i
should try it would be much appreciated. thanks

burnsy
Jul 23 '05 #1
2 1373
"mr_burns" <bi******@yahoo.co.uk> wrote in message
news:65**************************@posting.google.c om...
Hi,

I am trying to write code that will insert HTML into an empty <span>
tag. The function that will do this is add_item(), which works OK
where the code is inserted using the innerHTML command/method. The
problem occurs when i want to overwrite the code using a similar
function. it doesnt seem to do anything let alone overwrite. below is
a stripped down version of what i am tryin to achieve:

<html>
<head>
<script>

function add_item() {

item1.innerHTML = '<p>Insertion</p>';

}

function delete_item() {

item1.innerHTML = '<p>Insertion now overwritten</p>';

}

</script>
</head>
<body>
<span onClick="add_item()">Click here to insert</span><br><br>
<span onClick="delete_item()">Click here to delete</span><br><br>
<span id="item1"></span>
</body>
</html>
if anybody could tell me why it doesnt overwrite or another approach i
should try it would be much appreciated. thanks

The code you posted works like a charm in IE6, but doesn't work at all
in ie. Mozilla 1.6...

Replace your references to item1 with this:

function add_item() {
document.getElementById("item1").innerHTML = '...';
}

function delete_item() {
document.getElementById("item1").innerHTML = '...';
}

--
Dag.
Jul 23 '05 #2
Your code works perfectly in IE6 but not in Mozilla (I'm using 1.6). Here
are the modifications I made to get it to work in Mozilla, as well as IE:

function add_item() {
var myitem1;
myitem1 = document.getElementById("item1");
myitem1.innerHTML = '<p>Insertion</p>';
}

function delete_item() {
var myitem1;
myitem1 = document.getElementById("item1");
myitem1.innerHTML = '<p>Insertion now overwritten</p>';
}

using document.getElementById() will work in any DOM (Document Object Model)
compliant browser (at least, I'm pretty sure it will). Try to avoid
IE-specific code (I've been known to do this where necessary, but as a
general rule, stick to the DOM whereever possible).
Jul 23 '05 #3

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

Similar topics

3
by: soup_or_power | last post by:
Hi Sorry about the heading. I have a table with td consisting of lists with <select></select>. When I do a document.getElementById("element").innerHTML I don't see the selected item. IOW, the...
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...
7
by: Lorenzo Bettini | last post by:
Hi on the same page I set the innerHTML property of an element to something like my_string + "<br />" then when I try to remove my_string + "<br />" from the innerHTML it fails since the...
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 =...
8
by: Mike | last post by:
Hello, I keep reading about how NOT to use innerHtml in my Ajax applications, but no one suggests how to accomplish things without it.I'm looking for a tutorial, or does someone know of a good...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.