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

Question

Question

Copy data within a collection

asked on January 27, 2017

I have a form that fills in a collection of fields using a lookup (stored proc), but the users also have the ability to enter data manually (append) to the collection.  When a user appends data, some of the fields should copy down to the new 'row' and I am not sure how to do this.  In the example below, I would like the P/U Date & Due Date to copy from the first collection record, down to the second.  

I tried to copy the code found [Here] but I was not able to get it to work.  This is the basic idea, I just need it to happen within the same collection.  

Thanks,

Nate

0 0

Answer

SELECTED ANSWER
replied on February 1, 2017 Show version history

Hello Nate,

 

Here is some Javascript that will copy the value of the first item in a collection to the next item added in a collection.

$(document).ready(function() {
  $('a[ref-id="q1"]').click(function() {
    var elementCount = document.getElementsByName("q3").length;
    document.getElementById("Field3(" + elementCount + ")").value=document.getElementById("Field3(1)").value;
  });
});

This code assumes that the first input will always have a value when you hit the add button on the collection though.

Here is the example form that I will be using to explain the code:

The first line uses the ref id to locate the add button which is part of the collection to which I watch for a click on. From there I find the number of elements in the collection that I want to populate by searching for it's name which is "q3" in this case (the name was the same as the id for my case).

From here, you want to see the syntax for the input box that you want copied. This can be done by inspecting the input box in your browser which should look like this 

The id should be unique for each one, but we see that they are numbered (in this case, it is Field3(1)). Adding another part to the collection will make the next one have the id Field3(2).

From here, we know that the unique number comes from how many rows are in the collection so we can use the variable we created before that contains the number of rows in the collection. Therefore, we set the input box in the new row added to the value of the input box of the first row.

This example only shows one Single Line field, but can be expanded to other fields.

Hope this helps!

2 0
replied on August 8, 2017

Thanks Winston. This works great. In my case I just had to modify it a little bit so that it always takes the last inputted value instead of the first one. Thanks.

$(document).ready(function() {
  $('a[ref-id="q1"]').click(function() {
    var elementCount = document.getElementsByName("q3").length;
    document.getElementById("Field3(" + elementCount + ")").value=document.getElementById("Field3(" + (elementCount-1) + ")").value;
  });
});

Thank you,

Raul Gonzalez

0 0

Replies

replied on February 1, 2017

That worked perfect!  Thanks Winston!

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

Sign in to reply to this post.