2

I have a form having fields like:

<div class="row">
   <div class="field">
      <input class="" type="text" name="college" id="college"/>
   </div>

   <div class="field">
      <input class="" type="text" name="city" id="city"/>
   </div>

   <div class="field">
      <input class="" type="text" name="zip" id="zip"/>
   </div>
</div>
<input type="button" class="buttonWidth" id="btnAddressAdd" value="Add Worksite Addressess"/>

I have a Add extra address button that add's another copy of div "row" to the page. I need to send all data from the page as a request to the Controller. How do I write a script that add's extra div copy onclick of the button and also appends a unique id to each of the new fields?

3
  • Do you use javascript-frameworks like jQuery or Prototype? Commented Dec 14, 2012 at 11:59
  • I am working into a code base that uses plain javascript and DOJO for their inbuilt functions. I would have to stick to these coding practices itself. :( Commented Dec 14, 2012 at 12:02
  • retagged to dojo, those freaks will help you so hard. Commented Dec 14, 2012 at 12:23

3 Answers 3

2

See working example in Dojo: http://jsfiddle.net/phusick/PeQCN/

And the same code in plain vanilla JavaScript: http://jsfiddle.net/phusick/Rceua/

The Dojo version employs dojo/_base/lang::clone as @Peter Rader mentioned:

// var lang    = require("dojo/_base/lang");
// var array   = require("dojo/_base/array");
// var query   = require("dojo/query");
// var domAttr = require("dojo/dom-attr");

var counter = 0;

function duplicate(/*Node*/sourceNode, /*Array*/attributesToBump) {
    counter++;
    var out = lang.clone(sourceNode);
    if (domAttr.has(out, "id")) { out.id = bump(out.id); }

    query("*", out).forEach(function(node) {
        array.forEach(attributesToBump, function(attribute) {
            if (domAttr.has(node, attribute)) {
                domAttr.set(node, attribute, bump(domAttr.get(node, attribute)));
            }        
        })
    });

    function bump(/*String*/str) {
        return str + "_" + counter;
    }

    return out;
}

How to use the aforementioned duplicate function:

// var dom          = require("dojo/dom");
// var domConstruct = require("dojo/dom-construct");

var sourceNode = dom.byId("fieldset");
var node = duplicate(sourceNode, ["id", "name", "placeholder"]);  
domConstruct.place(node, sourceNode, "after");       
Sign up to request clarification or add additional context in comments.

Comments

0

I have written code to achieve this.

Logic:
1) get the innerHTML of desired parent
2) replace id in the text
3) Insert the new html

See a working code

Pardon me for bad style of coding on JS part. I am not use to coding directly on DOM. I prefer JQuery.

Comments

0

Try

to use this snipplet (see usage) http://dojotoolkit.org/reference-guide/1.8/dojo/_base/lang.html#dojo-base-lang-clone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.