0

Good day to all, I'm new to jQuery. I have this code.

$(document).ready(function() {
        var start_date = '';
        var end_date = '';
        var total = 0;

        $("#getdate").click(function () {

            start_date = parseInt($('#startdate').val().substring(5,7));
            end_date = parseInt($('#enddate').val().substring(5,7));
            total = (end_date - start_date) + 1;

            var monthDiv = $(document.createElement('div')).attr("id",'month' + total);

            for(i=1;i<=total;i++){
                monthDiv.after().html('<input type="text" />');
                monthDiv.appendTo("#monthList");
            }
            //alert(total); 
        });
});

Don't know where I went wrong but.. I want to generate textboxes base on the dates (start_date and end_date) so for example in the input type date I start with January and ends with March it should generate 3 textboxes. But in my code, it seems it will only generate one textbox per click. Any help would be much appreciated. Thanks!

And here's my html code:

<html>
<body>
   <input type="date" id="startdate" name="start_date" />
   <input type="date" id="enddate" name="end_date" />
   <input type="button" id="getdate" value="GET DATE"/>
   <div id="monthList">
   </div>
</body>
</html>
3
  • 1
    use monthDiv.append('<input type="text" />'); instead of monthDiv.after().html('<input type="text" />'); Commented May 5, 2013 at 7:06
  • Also use $('<div />') instead of $(document.createElement('div')) Commented May 5, 2013 at 7:59
  • Why? Can you explain further? Sorry about that. Commented May 5, 2013 at 8:04

3 Answers 3

1

You are doing an .after().html() which will always put only the last textbox.

http://jsfiddle.net/bpxVt/

$(document).ready(function() {
        var start_date = '';
        var end_date = '';
        var total = 0;

        $("#getdate").click(function () {

            start_date = parseInt($('#startdate').val().substring(5,7));
            end_date = parseInt($('#enddate').val().substring(5,7));
            total = (end_date - start_date) + 1;

            var monthDiv = $(document.createElement('div')).attr("id",'month' + total);

            for(i=1;i<=total;i++){
                monthDiv.after('<input type="text" />');

            }
             monthDiv.appendTo("#monthList");
        });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Why would you put $('#monthList').empty() what for? Anyway, thanks. It helped!
Oh! Sorry about that, I'm still new here. And about the emptying of textbox even though I didn't mentioned it. Thanks a lot!
1

See this: http://jsfiddle.net/nTwcH/

$(document).ready(function () {
  var start_date = '';
  var end_date = '';
  var total = 0;

  $("#getdate").click(function () {

    start_date = parseInt($('#startdate').val().substring(5, 7));
    end_date = parseInt($('#enddate').val().substring(5, 7));
    total = (end_date - start_date) + 1;

    var monthDiv = $(document.createElement('div')).attr("id", 'month' + total);

    for (i = 1; i <= total; i++) {
        monthDiv.append($('<input type="text" />'));
    }
    monthDiv.appendTo("#monthList");
  });
});

1 Comment

Thanks! I was planning to put names each of it.
0

The line monthDiv.after().html(''); should be written like monthDiv.html('');

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.