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

Question

Question

default a value on dropdown in collection

asked on August 23, 2018

Hi,

I am using the following code to default a value on a drop down within collection.  The drop down has values from a lookup table.  Also the 'defaultlocaton' field is outside the collection and it is a lookup value field.  The script is working to default the value on targeted collection drop-down field.  However, when a value is changed, I don't want the script to update the changed fields in the collection when a line gets appended.  It should only update the default value on the new appended line.  

Can someone please help with this script?

 

 

 

$('.cf-collection-append').click(function(){  
        grabVal()    //deafult value for each GL drop downns in line
  });

function grabVal() {
    var loc = $('.defaultlocation input').val();  
    var a = '';
       a = $('.cf-collection-block').find('.GLLoc option').filter(function () { return $(this).html() == loc;}).prop('selected', true);
}  

 

1 0

Replies

replied on August 23, 2018 Show version history

You can do a for each which will cycle through the fields GLLoc from top down (no need to even reference that they are in a collection). When using the .each loop, $(this) accesses the current field. Check if they already have a value, if not then set a default value.

Call this each time they click append.

 

$('.GLLoc option').each(function(){

var currentValue = $(this).val();

if(!currentValue){
//This field does not have a value yet
$(this).val(YOURDEFAULTVALUE);

}

});

 

 

1 0
replied on August 23, 2018

Hi Chad,

Tanks for your reply,

I tried this but couldn't get it going.  do I need to have a trigger?

I have a default value field populated with a location.  Within collection, I have a drop down that has a list from lookup.  When the location field has a value, I want that location to to be defaulted on the drop don.

 

Thanks for your help,

 

0 0
replied on August 23, 2018

I think I figure it.  Using Select on the drop down worked.  Thanks fro your help!

$('.GLLoc Select')
1 0
replied on August 23, 2018

Yes that looks right, select is the type for drop down fields. I have never heard of an option type before but I just expected it was something I had not used. Still so much javascript to learn.

Ironic how you must specify the field type but you can't specify a variable type. Javascript makes me crazy.

0 0
replied on August 23, 2018

You're absolutely right.  Thanks for all your help.

 

 

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

Sign in to reply to this post.