Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mozilla / firefox innerHTML problem

Status
Not open for further replies.

stinkybee

Programmer
Joined
May 15, 2001
Messages
218
Location
GB
I currently have a page that dynamically adds text boxes using innerHTML when required. All fine in ie but with firefox when a new box is added it clears any text you may have written in the previous box.

This suggests to me that firefox is adding the innerHTML to the original source code and not the current document code. Does anyone know how I can get firefox work with the actual content.

Here is a simple version of the code if you need it
Javascript:
Code:
function addBox() {
strBox = '<input type="text" value="">'
document.getElementById('imgBoxes').innerHTML+=strBox
}

HTML
Code:
<div id="imgBoxes">
</div>


So, when I add a box it appears ok. If I type something in the text box then add another the first one gets cleared. This only happens in Firefox.
 
add an element the way you're supposed to add an element, rather than using the innerHTML hack.

Code:
function addBox() {
    var b = document.createElement("input");
    b.type = "text";

    document.getElementById("imgBoxes").appendChild(b);
}



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
InnerHTML has been supported by ALL browsers since about May 2000. Whilst it was initially introduced by Microsoft in their IE browser, very swiftly the other browsers adopted it. Apart from the clunky nature of using normal DOM methods, innerHTML is actually faster in several browsers! cLFlaVA's code is spot on - don't get me wrong... but I just wanted to point out that innerHTML is hardly a hack [smile]

So... moving on... if you wanted to continue using an innerHTML approach, I would look at adapting your code a little to see if it's an easy fix:
Code:
function addBox() {
  var currentHTML = document.getElementById('imgBoxes').innerHTML;
  document.getElementById('imgBoxes').innerHTML = currentHTML +  '<input type="text" value=""/>';
}
Let us know how you go!

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Yeah, so basically cory was wrong, go figure.

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
kaht,

are you lonely lately? has the somewhat-recent birth of your first child left you feeling left-out? in need of attention? has your wife stopped coddling you? don't worry, pick on me relentlessly, you'll get all the attention you need.

headbang.gif
headbang.gif


Jeff,

ok, 'hack' was a strong word. i just feel like the innerHTML method is ghetto. i'm sure, however, that kaht and yourself will get nice [purple]lovin'[/purple] as always, as this site is fixed to ensure my failure.

[smile]



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I like the mohawk on the new headbanger icon. I'm sure if we could see behind it there would be a mullet
headbang.gif



[lol] why is that image hosted on pregnancy.org? That's awesome.

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
LOL! not too sure - I keep forgetting where the original headbanger was, although i seem to remember something like metalarchives.com or something, so i just search for "headbang smiley" any time i feel like rockin' out.



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
BabyJeffy

thanks for replying but that way still has the same effect. As in, it still blanks the first text box if you try to add another.

cLFlaVA
Your way works well especially in conjunction with an innerHTML. You can append a div and then write the innerHTML to it.

Thanks again guys
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top