innerHTML input fields: IE vs FF

S

SimonV

Hmm, I let the user add more input fields as needed. But I have a
strange problem with ff (and other mozillas)
When I add a field in ff, the others lost their content...
In IE the content stays
Is there a work-around for this?

this a piece of code to check this out:

<html>
<head>
<script type="text/javascript">
var counter = 0;
function doit(){
counter++;
document.getElementById('content').innerHTML += "<input type=\"text\"
id=\"field"+ counter +"\"/>";
}
</script>
</head>
<body>
<h1 id="header" onClick="doit()">Old header</h1>
<div id="content"><input type="text" id="jupse"/></div>
</body>
</html>
 
D

d

SimonV said:
Hmm, I let the user add more input fields as needed. But I have a
strange problem with ff (and other mozillas)
When I add a field in ff, the others lost their content...
In IE the content stays
Is there a work-around for this?

Have you tried using createElement() and appendChild()? That, to my
knowledge, is the most robust way to add inputs to a page...

dave
 
R

RobG

SimonV said:
Hmm, I let the user add more input fields as needed. But I have a
strange problem with ff (and other mozillas)
When I add a field in ff, the others lost their content...
In IE the content stays

You have discovered one of the differences between the implementation of
innerHTML in IE and Firefox - IE updates the value of the input in the
HTML, Firefox doesn't.

So when you read the innerHTML then write it back out again, IE keeps the
values that have been entered by the user, Firefox puts back the values in
the original source.

Is there a work-around for this?

Use DOM, not innerHTML.

this a piece of code to check this out:

<html>
<head>
<script type="text/javascript">
var counter = 0;
function doit(){
counter++;
document.getElementById('content').innerHTML += "<input type=\"text\"
id=\"field"+ counter +"\"/>";


Replace the line above with:

var div = document.getElementById('content');
var oI = document.getElementById('jupse').cloneNode(true);
oI.id = 'field' + counter;
oI.value = '';
div.appendChild(oI);

}
</script>
</head>
<body>
<h1 id="header" onClick="doit()">Old header</h1>
<div id="content"><input type="text" id="jupse"/></div>
------------------------------------------------^^^

Since you are using HTML, don't use XHTML-style empty element tag closures.
 
S

SimonV

I see, thanks for the replys.
But in fact it's a little more complicated than that...
I set a table with some input fields in it. The cloning isn't the right
solution for this because fields can change
I get the table and its content in a string. The append child doesn't
work with a string...
what now?
 
S

SimonV

Aha, I found a solution thats fine for me:

First I add a div to "jupse" and I give this div an id.
Then I use innerHTML on this new div to insert the string :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top