Clone and rename id and name
<p>I am trying to clone and rename id and names but the following code doesn't seem to be working. Could you please let me know if there are any suggestions?</p>

<p>It looks like the following line has problem.</p>

<p>cloned.id = what.attr('id') + "_" + cur_num; </p>

<p>Here is my function.</p>

<pre><code>       var cur_num = 1;    // Counter
    $("#btnClone").click(function(){

          var what = $("#MainConfig"); 
          var where = $("#smallConfig");

          var cloned = what.clone(true, true).get(0);
          ++cur_num;
          cloned.id = what.attr('id') + "_" + cur_num;                  // Change the div itself.

        $(cloned).find("*").each(function(index, element) {   // And all inner elements.
          if(element.id)
          {
              var matches = element.id.match(/(.+)_\d+/);
              if(matches &amp;&amp; matches.length &gt;= 2)            
                  element.id = matches[1] + "_" + cur_num;
          }
          if(element.name)
          {
              var matches = element.name.match(/(.+)_\d+/);
              if(matches &amp;&amp; matches.length &gt;= 2)            
                  element.name = matches[1] + "_" + cur_num;
          }

         });

       $(cloned).appendTo(where);

    });
});
</code></pre>
