Jump to content

Adding stuff in innerHTML, without clearing current fields data?


Recommended Posts

Hello,

 

I found nice way to add stuff in element innerHTML, but when I'm adding stuff in there, it clears everytime fields in it...

Is there way to update innerHTML, without losing data in fields?

 

Thanks for help.

 

Try using the += operator, like so:

var myDiv = document.getElementById('myDiv');
var moreText = "and, another thing.... ";

myDiv.innerHTML += moreText;

Hello, I'm new face in here...

Found this topic via Google, and this helped me kind of lot...

 

I've kind of code where should add new fields by clicking button, for sending more information to php-script.

<div id="areas">
  <p>Tiles between: <input name="minValues[]" type="text" id="min" size="5" /> - <input name="maxValues[]" type="text" id="max" size="5" /></p>
</div>

Using script below for onClick-action

function addRow() {
var elem = document.getElementById('areas');
var newText = '<p>\nTiles between:\n<input name="minValues[]" type="text" id="min" size="5" /> - <input name="maxValues[]" type="text" id="max" size="5" />\n</p>\n';
elem.innerHTML += newText;  
}

 

I've little problem, that if I've already entered some values to old fields, and add a new one,

all click will clear all older fields values also.

 

How might I could avoid that?

 

Added this in other (old solved) topic.

There you see my current script...

Hmm...I'm not sure.  I just wrote a simple test script (code below) that works fine:

 

<html>
<head>
   <title>blah</title>
   <script type="text/javascript">
      window.onload = function()
      {
         var myDiv = document.getElementById('myDiv');
         var myButton = document.getElementById('myButton');

         myButton.onclick = function()
         {
            myDiv.innerHTML += " button was clicked ";
         }
      }
   </script>
</head>

<body>
   <div id="myDiv"></div>
   <br />
   <br />
   <button id="myButton">Click Me</button>
</body>

</html>

 

Question: when exactly is your div losing the new text?  Because things set with innerHTML won't persist if you're refreshing the page due to a form submission.

Yeah, I'm not refreshing page...

But when I've input-fields in my element, with some values, that I've wrote in them...

And I notice that hey, I need another field for more information, and press button to add new field,

yeah, it will add there new field, but in same time, all earlier fields turn empty, and I'll have to write values in again.

Yeah, I'm not refreshing page...

But when I've input-fields in my element, with some values, that I've wrote in them...

And I notice that hey, I need another field for more information, and press button to add new field,

yeah, it will add there new field, but in same time, all earlier fields turn empty, and I'll have to write values in again.

 

Hmm...that's probably because the values written into the fields aren't considered to be a part of the existing HTML.  So, when you append the new HTML to the form, the values are lost because, as far as innerHTML is concerned, they don't exist anyway.

 

An easy fix is to temporarily store all the form's values in variables, then once the new HTML is appended to the old, reassign them to the proper fields.  Something like:

var myForm = document.getElementById('myForm');
var originalFormLength = myForm.elements.length;
var tmpValues = new Array();

for(var i = 0; i < originalFormLength; i++)
{
   tmpValues[i] = myForm.elements[i].value;
}

/* innerHTML assignment here */

for(var j = 0; j < originalFormLength; j++)
{
   myForm.elements[i].value = tmpValues[i];
}

 

It's a bit more difficult to do with a mix of form input types (i.e., text and/or radio buttons and/or a select element, etc), but you should get the idea.

What if you use the DOM instead of innerHTML?  Something like this:

 

function addRow() {

   var elem = document.getElementById('areas');
   var input_min = document.createElement('input');
   var input_max = document.createElement('input');
   var newText = document.createElement('p');

   input_min.setAttribute('type','text');
   input_min.setAtttribute('name','minValues[]');

   //Set the other attributes for input_min and input_max.  I'm too lazy to write it out. ;-)

   newText.appendChild(input_min);
   newText.appendChild(document.createTextNode(' - ');
   newText.appendChild(input_max);

   elem.appendChild(newText);
}

Check this shit out.

<script>
function blah()
{
	var f = document.forms[0];
	var a = new Array();
	for(var i = 0; i < f.length; i++)
	{
		a[i] = f.elements[i].value;
	}
	//do your reload
	for(var i = 0; i < a.length; i++)
	{
		f.elements[i].value = a[i];
	}
}
</script>
<form>
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="button" onclick="blah();" />
</form>

 

Did you mean something like that? If I've completely missunderstood what you were asking, please forgive me :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.