Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store object references in HTML elements?

I am building a form in HTML out of a javascript object that I've imported from a JSON file. I use a recurisve algorithm to build HTML tables, and respective elements (labels, text boxes, etc.) The fields load with the value of the current node.

The idea is to edit the values in the textboxes; which in turn updates the javascript object. When changes have been made, the editor will send the JSON object to the server and update the file.

The puzzling question, is how do I reference the node that has been changed? I have tried several approaches to no avail.

EDIT:

This is a basic idea of what I'm doing:

function build_tree(obj, depth) {
    for (key in obj) {
        if (typeof(obj[key]) == 'object') {
            print(key + "<input type="text" value='" + obj[key] + "'>");
            build_tree(obj[key], depth + 1);
        } else 
            print(key + "<input type="text" value='" + obj[key] + "'>");
}

Now, how do I bind the value of obj[key] to the text boxes, so that when I change the value it updates the Javascript object?

like image 759
JohnnyStarr Avatar asked Jan 26 '12 19:01

JohnnyStarr


People also ask

What is object element HTML?

Definition and Usage. The <object> tag defines a container for an external resource. The external resource can be a web page, a picture, a media player, or a plug-in application. To embed a picture, it is better to use the <img> tag. To embed HTML, it is better to use the <iframe> tag.

What is an object element?

An object element contains elements for the object fields. Each object element can contain one or more attributes. The following table lists the attributes that can apply to an object element. Attribute names and values are case-sensitive. All attributes are optional.

Can you store an object in an object JavaScript?

In summary, we can store JavaScript objects in localStorage by first converting them to strings with the JSON. stringify method, then back to objects with the JSON. parse method.


1 Answers

document.getElementById('name').changed = true;

So now the DOM element has the property 'changed'. You can also use any other value (dates, arrays, etc)

like image 96
Ivan Castellanos Avatar answered Oct 10 '22 09:10

Ivan Castellanos