Clone and validation on button click
<p>I am trying to clone the part of my form. The following code works fine but I would like to validate the input of Radio button and dropdown box before cloning and appending it to the form. Also how to 'remove' the last cloned section when I click on 'Remove' button? I appreciate any input.</p>

<pre><code>&lt;fieldset class="clonable"&gt;  
    &lt;div class="regrow"&gt;
        &lt;input type="text" id="fname_1" /&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;input type="radio" id="gender_1" name="gender" value="M" onclick="toggle()"&gt;Male&lt;br&gt;
        &lt;input type="radio" id="gender_2" name="gender" value="F" onclick="toggle2()"&gt; Female
    &lt;/div&gt;
    &lt;div&gt;
        &lt;input type="text" id="lname_1" /&gt;
    &lt;/div&gt; 
    &lt;div&gt;
        &lt;select id="select_1" name="select_1"&gt;
            &lt;option&gt;&lt;/option&gt;
            &lt;option value="B"&gt;B&lt;/option&gt;
            &lt;option value="R"&gt;R&lt;/option&gt;
        &lt;/select&gt;
    &lt;/div&gt;               
&lt;/fieldset&gt;   
&lt;div id="formbuttons" class="regrow"&gt; 
    &lt;input type="button" class="button" id="clonetrigger" value="Clone" /&gt;
    &lt;input type="button" class="button" id="remove" value="Remove" /&gt; 
&lt;/div&gt;
</code></pre>

<p>JQuery code</p>

<pre><code>$(document).ready(function(){

    $('#clonetrigger').click(function(){
        var yourclass = ".clonable";  
        var clonecount = $(yourclass).length;   
        var newid = Number(clonecount) + 1;     
        $(yourclass+":first").fieldclone({      
            newid_: newid, //Id of the new clone, (you can pass your own if you want)
            target_: $("#formbuttons"),
            insert_: "before",                  
            limit_: 4
        });
        return false;
    });

});

(function($) {

    $.fn.fieldclone = function(options) { 

        var settings = {
            newid_ : 0,
            target_: $(this),
            insert_: "before",
            limit_: 0
        };
        if (options) $.extend(settings, options);           
        if( (settings.newid_ &lt;= (settings.limit_+1)) || (settings.limit_==0) ){    
                var fieldclone = $(this).clone();
            var node = $(this)[0].nodeName;
            var classes = $(this).attr("class");

            var srcid = 1;
            $(fieldclone).find('input, select, textarea').each(function(){
                var s = $(this).attr("name");           
            $(this).attr("id", s.replace(eval('/_'+srcid+'/ig'),'_'+settings.newid_)); 
                });

            var targetid = $(settings.target_).attr("id");
            if(targetid.length&lt;=0){
                targetid = "clonetarget";
            $(settings.target_).attr("id",targetid);
            }       

                var newhtml = $(fieldclone).html().replace(/\n/gi,"");
                newhtml = '&lt;'+node+' class="'+classes+'"&gt;'+newhtml+'&lt;/'+node+'&gt;';

            eval("var insertCall = $('#"+targetid+"')."+settings.insert_+"(newhtml)"); 
        }
    };

});
</code></pre>
