Search code examples
jquerytimepicker

How to create a dynamic text box with time picker


I am working in a an application where i have to generate multiple textboxes and also i need to add timepicker into them .Now in my application i am able to create the mutiple textboxes dynamically.But i am not able to put timepicker on that.Here is my code

$(function () {
$("#btnAdd").bind("click", function () {
    var div = $("<div />");
    div.html(GetDynamicTextBox(""));
    $("#TextBoxContainer").append(div);
});
$("#btnGet").bind("click", function () {
    var valuesarr = new Array();   
    var phonearr = new Array();  
    var phonearr1 = new Array(); 
    $("input[name=DynamicTextBox]").each(function () {
        valuesarr.push($(this).val());
        $('#DynamicTextBox').val(valuesarr);
    });
    $("input[name=phoneNum]").each(function () {
        phonearr.push($(this).val());
        $('#phoneNum').val(phonearr);
    });

    $("input[name=phoneNum1]").each(function () {
        phonearr1.push($(this).val());
        $('#phoneNum1').val(phonearr1);
    });

    alert(valuesarr); alert(phonearr);alert(phonearr1);
});

$("body").on("click", ".remove", function () {
    $(this).closest("div").remove();
});

});


function GetDynamicTextBox(value) {
   return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />&nbsp;<input id="myPicker" class="time" type="text" />&nbsp;<input name = "phoneNum1" type="text" /><input type="button" value="Remove" class="remove" />';
}

Here is the fiddle i want to add two timepickers in the 2nd and 3 rd box

DEMO Somebody pLease help


Solution

  • USE THIS: $("#myPicker").timepicker(); //add this on Add click.if you are willing to create multiple time picker better use class.you can use : $(".time").timepicker(); also where time is the class attached to the input field.

    $(function () {
        $("#btnAdd").bind("click", function () {
            var div = $("<div />");
            div.html(GetDynamicTextBox(""));
            $("#TextBoxContainer").append(div);
           // $("#myPicker").timepicker();//add this
            $(".time").timepicker();
        });
        $("#btnGet").bind("click", function () {
            var valuesarr = new Array();   
            var phonearr = new Array();  
            var phonearr1 = new Array(); 
            $("input[name=DynamicTextBox]").each(function () {
                valuesarr.push($(this).val());
                $('#DynamicTextBox').val(valuesarr);
            });
            $("input[name=phoneNum]").each(function () {
                phonearr.push($(this).val());
                $('#phoneNum').val(phonearr);
            });
            
            $("input[name=phoneNum1]").each(function () {
                phonearr1.push($(this).val());
                $('#phoneNum1').val(phonearr1);
            });
           
            alert(valuesarr); alert(phonearr);alert(phonearr1);
        });
        
        $("body").on("click", ".remove", function () {
            $(this).closest("div").remove();
        });
        
    });
    
    function GetDynamicTextBox(value) {
           return '<input name = "DynamicTextBox" type="text" value = "' + value + '" />&nbsp;<input id="myPicker" class="time" type="text" />&nbsp;<input name = "phoneNum1" id="phoneNum1" class="time"  type="text" /><input type="button" value="Remove" class="remove" />';
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://jonthornton.github.io/jquery-timepicker/jquery.timepicker.js"></script>
    <link rel="stylesheet" type="text/css" href="http://jonthornton.github.io/jquery-timepicker/jquery.timepicker.css">
    <div id="TextBoxContainer">
    <input id="btnAdd" type="button" value="Add" />
    </div>