You are viewing limited content. For full access, please sign in.

Question

Question

Copy values from one field to another

asked on June 17, 2014

I have several forms that will have the same radio button choices.  Then depending on the choice, I would like to pull the first and last name out for further use from a hidden field.  We use a database for staff, a different one for students, and the student teachers are just write ins.

 

The radio button will open up different sections for a look up in the different databases depending on the choice.  Once the information is acquired I felt the easiest way to have a common thread to get the name for later would be in a hidden area.  That way on the business process I would only use the hidden area to call the name. 

 

Also, while I am at it, another I have is I have parsed the date field also in the hidden area to make it easier for filing away.  What I would actually prefer is to use the date field at the top, and if it is after July 1st parse the year to Year +1 in the hidden area, if before July 1, then keep the current year.  That way I can save the form under the current school year. 

 

I will include the form as I have it so far.

 

0 0

Answer

SELECTED ANSWER
replied on June 18, 2014 Show version history

How dynamic? You basically just want to ensure that it is June 1st of the current year?

 

$(document).ready(function () {
    $('.name input').blur(function () {
        $('.otherName input').val($(this).val());
    });

    $('.date input').change(function () {
        var thisYear = new Date().getFullYear();
        var today = new Date($(this).val());
        var cutoffDate = new Date("6/1/" + thisYear);
        if (today >= cutoffDate) {
            $('.otherDate input').val(Number($(this).val().slice(-4)) + 1);
        } else {
            $('.otherDate input').val(Number($(this).val().slice(-4)));
        }
    });

});

 

0 0

Replies

replied on June 18, 2014

You can use JavaScript to copy the value from one field to another and to manipulate that value if you like. In your example you have two sets of fields for the name and date. I like to assign these fields CSS classes. Give the name field the name CSS class, the date field the date CSS class, the hidden name field the otherName CSS class, and the hidden date field the otherDate CSS class.

 

If you only want to exactly copy the value from one field to another, you just need to wait for the user to leave that field and then copy the value. If you want to manipulate the value, as you do with the date field, you'll still need to wait for the field to change before you modify the value.

 

$(document).ready(function () {
    $('.name input').blur(function () {
        $('.otherName input').val($(this).val());
    });

    $('.date input').change(function () {
        var today = new Date($(this).val());
        var cutoffDate = new Date("6/1/2014");
        if (today >= cutoffDate) {
            $('.otherDate input').val(Number($(this).val().slice(-4)) + 1);
        } else {
            $('.otherDate input').val(Number($(this).val().slice(-4)));
        }
    });
});

If you are adding CSS classes to fields you want to hide, avoid hide, hidden, and invisible. Applying these classes will make the field invisible even in the Form Designer.

0 0
replied on June 18, 2014

Thank you Eric,

Is there a way to make the date more dynamic so that when the year changes I wont have to remember to change the year in the JavaScript?

0 0
SELECTED ANSWER
replied on June 18, 2014 Show version history

How dynamic? You basically just want to ensure that it is June 1st of the current year?

 

$(document).ready(function () {
    $('.name input').blur(function () {
        $('.otherName input').val($(this).val());
    });

    $('.date input').change(function () {
        var thisYear = new Date().getFullYear();
        var today = new Date($(this).val());
        var cutoffDate = new Date("6/1/" + thisYear);
        if (today >= cutoffDate) {
            $('.otherDate input').val(Number($(this).val().slice(-4)) + 1);
        } else {
            $('.otherDate input').val(Number($(this).val().slice(-4)));
        }
    });

});

 

0 0
replied on June 18, 2014

Correct.  Whether the current year is 2014 or 2015 etc.

0 0
replied on June 18, 2014

Thank you!!

Almost everything we have is stored by school year and this will work GREAT!!

0 0
replied on June 18, 2014

You're welcome!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.