0

I am building a dynamic form creator that allows users to add form elements and change the properties. I need to find a way to store this data on the client side without the complexity of XML or JSON. The user could add 50 form elements from text box to radio to textarea. Each element has a different number of changeable variables.

I am currently storing them in hidden fields values like:

type:text, size:30, required:yes, top:30, left:30

type:textarea, cols:30, rows:5, top:50, left:60

I am using jquery to add each item and the hidden.

var typeVariable = 'type:input';

var startPosLeft =  'left:' + Math.round($(newElem).offset().left$('.container').offset().left); 

var startPosTop =  'top:' + Math.round($(newElem).offset().top - $('.container').offset().top); 

var newElemValue = typeVariable + ',' + startPosTop + ',' + startPosLeft;

// creates hidden form elements to store data
$('<input>').attr({'type': 'text', value:newElemValue, 'name':'hidden' + newNumDivs,  id:'hID' + newNumDivs}).appendTo('.hiddenDiv');
  1. Is this the best way to store and retrieve data without doing a ton of ajax calls

  2. How can I call a specific element from a string like:

    type:text, size:30, required:yes, top:30, left:30

I will need 'text' not 'type:text' .

How can I update a specific element in this string? If size:30 changes to size:50 how do I change this data stored in a hidden field and insert it into

type:text, size:50, required:yes, top:30, left:30
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
user1009749
  • 63
  • 3
  • 8
  • jSON would be the best way to store these values on the client side; it also has the benefit of allowing you to easily send it to the server to persist in a database. Is there a reason you are so adverse to using jSON, especially since the notation is not much different than the one you are currently using? – rkw Nov 08 '11 at 01:40
  • I guess, its because I am not that familiar with jSON and was trying to keep it as simple as possible. I was trying things like --String (typeVariable[1]).split(':')[1]; – user1009749 Nov 08 '11 at 01:45
  • Don't use JSON to store the information while you're using it, use a JavaScript object/array as per my answer. Then when you want to submit it to the server (I'm assuming you do so eventually, when the user is finished) you can serialise the object as JSON and submit it via a conventional form or via Ajax. There's no way that all of the individual inputs and strings that you are trying to use above will be "as simple as possible". Everything in my answer is basic JavaScript; I get that as a novice you may not understand it all yet, but you need to learn it... – nnnnnn Nov 08 '11 at 01:58
  • You actually already know JSON. The differences are important, but minimal. JSON is a subset of javascript objects: http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation – rkw Nov 08 '11 at 02:26

1 Answers1

1

You should be storing all of that information in a JavaScript array, where each element in the array is an object with the appropriate properties to represent one of the form elements your user is creating. You can easily add elements and update properties of existing items.

// create empty array
var formElements = [];

// add a new element to the array
formElements.push( {
   type : "text",
   size : 50,
   required : "yes",
   top : 30,
   left : 30  
} );

// individual items in the array can have different properties,
// e.g., where the one above had "size" this one has "cols":
formElements.push( {
   type : "textarea",
   cols : 30,
   rows : 5,
   top : 50,
   left : 60       
} );

// later access the rows property of the 2rd element:
var rows2nd = formElements[1].rows;

// later change the size property of the 1st element:
formElements[0].size = 50;

// loop through the array and log the type of each element:
for (var i=0; i < formElements.length; i++) {
   console.log(formElements[i].type);
}

When your user is ready to submit you can serialise the array to JSON and submit in a conventional form field or via Ajax.

UPDATE prompted by your latest comment. To "name the array elements" you use an object instead of an array:

var formElements = {};

formElements["textTemplate"] = {
   type : "text",
   size : 50,
   required : "yes",
   top : 30,
   left : 30  
}
formElements["textareaTemplate"] = {
   type : "textarea",
   cols : 30,
   rows : 5,
   top : 50,
   left : 60  
};

Or you can define them all in a single statement (note the nested curly braces):

var formElements = {
        "textTemplate" :    {
                               type : "text",
                               size : 50,
                               required : "yes",
                               top : 30,
                               left : 30  
                             },    
        "textareaTemplate" : {
                               type : "textarea",
                               cols : 30,
                               rows : 5,
                               top : 50,
                               left : 60
                             }
};

Then to get, say, the "top" property for a "textareaTemplate" you say:

formElements["textareaTemplate"]["top"];`
// or in dot notation
formElements.textareaTemplate.top;

To loop through all items in the object say:

var currentItem;
for (var key in formElements) {
   currentItem = formElements[key];
   alert(currentItem["top"]);
}

For more information see https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Thank you. The only question I have is will formsElements.push just keep creating new array elements from 0+? So If I have 20 text fields and 20 list menus, every time one items is added is will create formElements[0] - [1] -[2] and so on? – user1009749 Nov 08 '11 at 02:09
  • Does this allow for additional array levels? Example: [cols : 30 : 'Columns', , – user1009749 Nov 08 '11 at 02:19
  • Yes, `.push()` just adds an item to the end of the array (starting from element 0 if the array is empty). From the example in your second comment I'm not sure quite what you're trying to do, but you can definitely nest arrays within arrays, or have an object with a property that is another object or an array, as many levels down as you like. You need to understand the difference between an array, which has items accessed via numeric indexes, and an object, which properties that are key/value pairs. Have a look at https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects – nnnnnn Nov 08 '11 at 02:35
  • yup, 8-5er trying to build this site at night and learn on the fly. Link was very helpful. – user1009749 Nov 08 '11 at 03:18
  • I had one more question. The example works perfect, but how can I use it as a template? I will have 12+ form element types and would like to avoid 12+ 'if' statements. So how can I call the 'formElements' on a click for adding the new form element? I was also wondering how I can either name the array element or use my own number instead of 0,1,2,3,4,5 etc. – user1009749 Nov 09 '11 at 23:21
  • OK, I've added more info to the end of my answer. Don't forget to accept an answer once you have one that you're happy with (click the tick to the left of the answer: it'll turn green). – nnnnnn Nov 09 '11 at 23:46