0

I'm trying to copy the input data of multiple fields into one big one, for date of birth.

day value + month value + year value = day value/month value/year value into one other field all together. I made variables of each field and then try to add them in the 'full' input field, but this doesn't work. What am I doing wrong?

Demo: http://jsfiddle.net/J2PHq/

$(function(){
    $('.copy').on('keyup blur', function(){
         $('.full').val(day + '/' + week + '/' + year);

        day = $(".day").val();
        week = $(".week").val();
        year = $(".year").val();
     }).blur();
});
2
  • you're fist appending the values and then getting them which is the wrong order, also you might want to use var to define variables Commented Mar 28, 2014 at 13:19
  • updated working fiddle jsfiddle.net/J2PHq/4 Commented Mar 28, 2014 at 13:20

2 Answers 2

1

You need to declare the variables before you enter them in the .full field.

Working fiddle: here

$(function(){
    $('.copy').on('keyup blur', function(){        
        var day = $(".day").val();
        var week = $(".week").val();
        var year = $(".year").val(); 

        $('.full').val(day + '.' + week + '.' + year);

     }).blur();
});
Sign up to request clarification or add additional context in comments.

1 Comment

It is always good to have both in the answer, so all users may get the solution at any time whenever JSFiddle.net works or not.
1

Wrong execution order -

$(function(){
    $('.copy').on('keyup blur', function(){
        var day = $(".day").val();
        var week = $(".week").val();
        var year = $(".year").val();
        $('.full').val(day + '/' + week + '/' + year);
     }).blur();
});

Demo ---> http://jsfiddle.net/J2PHq/6/

2 Comments

Say NO to global variables!
Thanks, indeed the execution order.

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.