jQuery date split into day month year I'm changing an HTML datepicker value (#date) into 3 separate values in order to post them to a booking engine. I've managed to get it to work but there is a lot of repetition and the original #date value is also submitted unnecessarily.

The engine takes these parameters: 

date_day=27&amp;date_month=9&amp;date_year=2013&amp;nights=3&amp;people=2&amp;ref=558&amp;langClient=eng&amp;expr=EUR`


HTML

 &lt;form id="quick_res" action="#" method="post" name="availability"&gt;            
            &lt;ul&gt;    
                &lt;li&gt;
                &lt;label for="date" id="anchor"&gt;Date :&lt;/label&gt;&lt;input type="date" name="date" id="date" placeholder="12/09/13"&gt;
                &lt;/li&gt;

                &lt;li&gt;
                &lt;label for="numNights"&gt;Nights :&lt;/label&gt;&lt;input type="number" name="nights" id="nights" placeholder="2"&gt;
                &lt;/li&gt; 

                &lt;li&gt;
                &lt;label for="numGuests"&gt;Guests :&lt;/label&gt;&lt;input type="number" name="people" id="people" placeholder="2"&gt; 
                &lt;/li&gt;                                       

                &lt;input name="ref" type="hidden" id="ref" value="558" /&gt; 
                &lt;input name="langClient" type="hidden" id="langClient" value="eng" /&gt;
                &lt;li class="select-container"&gt;
                &lt;label for="currency"&gt;Currency :&lt;/label&gt; 
                    &lt;select name="expr" id="expr"&gt;
                        &lt;option value="EUR" selected="selected"&gt;EURO&lt;/option&gt;
                        &lt;option value="USD"&gt;US Dollar&lt;/option&gt;
                        &lt;option value="ARS"&gt;Argentine Peso&lt;/option&gt;
                        &lt;option value="AUD"&gt;Australian Dollar&lt;/option&gt;
                        &lt;/select&gt;
                &lt;/li&gt;
                &lt;/ul&gt;

                &lt;input type="submit" name="submit" class="secondary button radius text-center" value="Click to Book"&gt;                    


    &lt;/form&gt;


jQuery

 $('#date').on('change', function() {
     var new_val = $(this).val().split('-'),
         dateYear = parseInt(new_val[0]),
         dateMonth = parseInt(new_val[1]),
         dateDay = parseInt(new_val[2]);

    $('&lt;input&gt;', {
        type: 'hidden',
        name: 'date_day',
        id: 'date_day',
        value: dateDay

    }).appendTo('#anchor');

    $('&lt;input&gt;', {
        type: 'hidden',
        name: 'date_month',
        id: 'date_month',
        value: dateMonth

    }).appendTo('#anchor');

    $('&lt;input&gt;', {
        type: 'hidden',
        name: 'date_year',
        id: 'date_year',
        value: dateYear

    }).appendTo('#anchor');

 });
The tought process to make your code DRY is almost always the same:


Find the commonalities between blocks of code.
Extract these into an iterable structure.
Loop.


Here we can see that everything is the same except the prefix and the value.

The code could be changed to:

Please note that you should always specify the radix argument when using the parseInt function since 10 is not default in every browser.

$('#date').on('change', function() {
    var newVal = $(this).val().split('-'), //renamed new_val to newVal, always stick to one naming convention
        dateParts = {
            year: parseInt(newVal[0], 10),
            month: parseInt(newVal[1], 10),
            day: parseInt(newVal[2], 10)
        };

    $('#anchor').append($.map(dateParts, function (index, key) {
        var name = 'date_' + key;

        return $('&lt;input&gt;', {
            type: 'hidden',
            name: name,
            id: name,
            value: dateParts[key]
        });
    }));
});


EDIT:

Note that with the above logic, you will end up having duplicate inputs with different values if the user changes the value multiple times. You could solve this by removing the previously added inputs before adding the new ones, but you could also simply add the inputs just prior to form submission by listening to the submit event instead.

Also, if you want to avoid sending #date's value, you can disable that input before submitting; disabled inputs aren't sent to the server.

Finally, you could choose to simply submit the form in AJAX using $.ajax (or any of it's shortcut methods such as $.post). That would have the advantage of making your site slighlty more dynamic and you would not have to append inputs to the form since you can use the data option.