Lopofsky
Lopofsky

Reputation: 528

Jquery/Javascript - Dynamicly Create Forms with Datepicker

I'm new to js/jquery and I'm trying to create dynamically html forms with jquery but I can't figure out how to "repeat" (sorry that I don't know the probationary term) the "datepicker" function of the jquery.

Below is the html:

<html lang="en">
  <head>

  <meta charset="utf-8">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">


<script>
         $(document).ready(function(){
            var x = 0;
            $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
            $("#add").click(function(){
                x++;
                var dateFuncName = "#datepicker" + x.toString();
                var dateFuncName2Call = "datepicker" + x.toString();
                $(dateFuncName).datepicker({dateFormat: 'yy-mm-dd'});
                $("#name_data").append('<p>First Name: <input name="name['+x+'][first]" type="text"> Last Name: <input name="name['+x+'][last]" type="text"> Date: <input id="' + dateFuncName2Call + '" name="name['+x+'][date]" type="text"> <input id="remove'+x+'" type="button" name="remove" value="remove"></p>');
                $("#remove"+x).click(function(){
                    $(this).parent('p').remove();
                });
            });
          });
</script>


 <form action="test.php" method="post">
            <div id="name_data">
                <p>First Name: <input name="name[0][first]" type="text">
                   Last Name: <input name="name[0][last]" type="text">
                   Date: <input id="datepicker" name="name[0][date]"></p>
            </div>
            <p><input id="add" type="button" name="add" value="Add more"></p>
            <p><input type="submit" name="submit" value="Submit..."></p>
        </form>


</html>

Specifically what I've figured from a previous implementation is that for each datetime field in html I need a new "#datetime" iteration (i.e. #datetime2, 3 etc), this worked fine for a predefined number of fields.

That is what I've tried to do here, specifically at this point of the above code:

x++;
var dateFuncName = "#datepicker" + x.toString();
var dateFuncName2Call = "datepicker" + x.toString();
$(dateFuncName).datepicker({dateFormat: 'yy-mm-dd'});

Please excuse the lame use of Jquery here, but I'm really noob at the moment :D

Thank you in advance for any guidance/advice :D

Upvotes: 0

Views: 51

Answers (2)

Franklin Satler
Franklin Satler

Reputation: 320

If you are trying to clone your fields, this is the way to go.

$(document).ready(function(){
    $("#add").click(function(){
        var $cloneFields = $("#name_data").clone(); // Clone your 3 inputs into a variable
        $cloneFields.find(":input").val('');        // Clear the fields of this variable
        $($cloneFields).insertAfter("#name_data");  // Insert your array of DOM elements after your cloned div
    });
});

Upvotes: 0

Diptox
Diptox

Reputation: 1809


$(document).ready(function(){
    var x = 0;
    $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
    $("#add").click(function(){
        x++;
        var dateFuncName = "datepicker" + x.toString();
        $("#name_data").append('<p>First Name: <input name="name['+x+'][first]" type="text"> Last Name: <input name="name['+x+'][last]" type="text"> Date: <input id="' + dateFuncName + '" name="name['+x+'][date]" type="text"> <input id="remove'+x+'" type="button" name="remove" value="remove"></p>');
        $("#"+dateFuncName).datepicker({dateFormat: 'yy-mm-dd'});
        $("#remove"+x).click(function(){
            $(this).parent('p').remove();
        });
    });
});

Upvotes: 1

Related Questions