473,513 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating html from javascript objects

Hi all,

Does anyone know if there is an easy way to create the html
representation of an HTMLElement object in javascript? I'm attempting
to update the innerHTML property of a div element to allow user entries
to presist when the the input display pane is changed. Basically I have
several different div panes that users can rotate through when entering
values of a simulated form whose values are then parsed and used to
dynamically display content using ajax style methods. The innerHTML
property seems to update in IE as users input values and this works
fine. However, with firefox a user's values are lost when I update the
innerHTML property of one of the hidden div elements. So basically I'm
trying to write a method that will build the updated html with
checkboxes checked etc so that I can use the generated html as the
value of the innerHTML property of the div I'm trying to update.

May 12 '06 #1
3 1876

lc******@gmail.com wrote:
(snipped)
However, with firefox a user's values are lost when I update the
innerHTML property of one of the hidden div elements. So basically I'm
trying to write a method that will build the updated html with
checkboxes checked etc so that I can use the generated html as the
value of the innerHTML property of the div I'm trying to update.


I had this problem, I solved it by reading in all the data, updating
the innerHTML, then writing the values back. Something like this :

var controls = ['name', 'address', 'number_of_badgers'];

function UpdateForm(form)
{
// first save all the values
var saved = [];
for(var control_name in controls)
{
saved[control_name] = form[control_name].value;
}

// mess around with form :
form.innerHTML += "<p>Message</p>";

// save the values back
for(var control_name in controls)
{
form[control_name].value = saved[control_name];
}
}

HTH

Sam

May 12 '06 #2
lc******@gmail.com wrote:
Hi all,

Does anyone know if there is an easy way to create the html
representation of an HTMLElement object in javascript?
innerHTML is a property invented by MS and widely copied. There is no
public specification, so others have simply copied MS's implementation
with varying degrees of exactitude.

It generally works OK except that not all DOM attributes are represented
in HTML, for example you can't differentiate between current value and
default value for a text input, you can only set the value attribute:

<input ... value="foo">

which now becomes the default value and the current value. In Firefox,
the innerHTML value attribute remains as per the original source HTML,
but in IE it becomes whatever the current value is. So in Firefox you
lose the current value, in IE you lose the default value.

I'm attempting
to update the innerHTML property of a div element to allow user entries
to presist when the the input display pane is changed. Basically I have
several different div panes that users can rotate through when entering
values of a simulated form whose values are then parsed and used to
dynamically display content using ajax style methods. The innerHTML
property seems to update in IE as users input values and this works
fine. However, with firefox a user's values are lost when I update the
innerHTML property of one of the hidden div elements. So basically I'm
trying to write a method that will build the updated html with
checkboxes checked etc so that I can use the generated html as the
value of the innerHTML property of the div I'm trying to update.


Have you considered hiding the forms using the display attribute? Or
removing them from the document and storing them in an object? Then you
can replace them whenever you want. Serialising elements using
innerHTML then writing them back out again does not seem to be a good idea.

Try this sample that removes the object by storing it in a global
object, then restores it. When archived, the form is kept as-is. It is
still available to script if you want to do anything with it but it's no
longer part of the document.

<script type="text/javascript">

var elStore = {};

function archiveEl(id) {
var el = document.getElementById(id);
elStore[id] = el.parentNode.removeChild(el);
}

function replaceEl(childID, parentID) {
if (childID in elStore) {
var p = document.getElementById(parentID);
p.appendChild(elStore[childID]);
}
}
</script>

<button onclick="archiveEl('formA');">Remove formA</button>
<button onclick="replaceEl('formA','frameA');">Restore formA</button>

<div id="frameA">
<form id="formA" name="formA" action=""><div>
<input type="text" name="t0" value="t0 default"><br>
<input type="text" name="t2" value="t0 default"><br>
<input type="radio" name="rset0" value="rs0" checked><br>
<input type="radio" name="rset0" value="rs1"><br>
<input type="radio" name="rset0" value="rs2"><br>
<input type="submit"><input type="reset">

</div></form>

</div>

--
Rob
May 13 '06 #3
Rob,

Thanks for all the info. I figured it was something to do with the
various implementations of innerHTML, but its good to have that bit of
knowledge clarified. In the end, using the divs themselves instead of
using them as containers ended up doing the trick. I have to set width
and height to make sure they're truly hidden when they need to be but
all in all it seems I was really overcomplicating the issue.

Thanks again,

Larry

May 15 '06 #4

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

Similar topics

4
8387
by: VK | last post by:
09/30/03 Phil Powell posted his "Radio buttons do not appear checked" question. This question led to a long discussion about the naming rules applying to variables, objects, methods and properties...
20
3085
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
2
8177
by: Frederik S | last post by:
Hello, I'm making a nice little login box in Javascript but having problems Posting the value inside a textfield. In a nutshell: I have a function: function getPostData (value)
6
2933
by: NotGiven | last post by:
I want to learn moer of what I saw in a recent example. They create a page that created new fields/element. It's not like they were hidden and they displayed them, they were not there, then the...
5
2264
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
2
3035
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
9
2053
by: William | last post by:
Heya, I have been looking all over Google for ways to *create* an XML document using any Javascript object, and it seems that no one has ever tried or ever needed this. I want to send the...
1
8241
by: kret | last post by:
Hi, this is my first post so first of all I would like to say hello :) Now getting to my problem. In my job I have to create an ActiveX control in .NET 1.1 that can be lunched from IE....
1
2471
by: joe_doufu | last post by:
I'm creating a page with multiple "widgets", each with its own XMLHttpRequest object, so the user can play with the widgets in parallel. The widgets are enclosed in divs with class "widget" and a...
0
7161
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
7384
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
7539
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...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
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
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.