Solved

jQuery to get value of control in last row of Repeating Section

  • 8 January 2019
  • 9 replies
  • 186 views

Badge +5

Using jQuery how do I get the value of a field in the last row of a repeating section:

 

 

In the screenshot above the Type fields are lookup controls with a class of .ItemType

 

I would need to return a value of "Software".

icon

Best answer by chaddavis 8 January 2019, 18:40

View original

9 replies

Badge +7

If you give the Type control a CSS class (like myField), you should be able to use this code:

var myFields = NWF$('.myField');
var lastValue = myFields[myFields.length - 1].value;

The 'lastValue' variable should have the last field's value.

Of course, you're going to need some sort of trigger, because if you run it on form load, the filed will be empty in a new form. It won't have a value until someone enters it. 

Badge +5

Thanks for responding. I'm using Chrome console at the moment to test my jQuery.

Your suggestion returns 'undefined'.

My Type controls have a class of .ItemType (i've updated the original post to reflect this).

I can see that we can target the last row in the repeating section using the following code:

NWF$('.nf-repeater-row:last')...;

And I've used different variations to try and extract the value but with no success:

NWF$('.nf-repeater-row:last .ItemType').val();
NWF$('.nf-repeater-row:last').val('.ItemType');
NWF$('.nf-repeater-row:last .ItemType').find('option').val();

etc...

Badge +7

I was also using Chrome console to test, and it worked for me. I would suggest playing around with it a little to see what is being returned. For example, change 'myFields.length - 1' to a number. You could also simply type myFields.length to ensure it's being populated. Test those and see what you get.

Badge +5

Are you using a choice control or a lookup control for the drop down?

Badge +5

I'll have a play around as you said. If I type myFields.length is does return the correct number of rows, so I can see what you're doing. I'll let you know if this leads me to the solution

Badge +7

My apologies. I used a Text field. I didn't realize you were using a Lookup field.


Try this code instead:

var myFields = NWF$('.myField option:selected');
var lastText = myFields[myFields.length - 1].text;
Badge +5

It worked. Just had to change the value to -2.

var lastField = NWF$('.ItemType option:selected');
var lastText = lastField[lastField.length - 2].text;‍‍‍‍‍‍‍

Thank you so much.

Please i have repeating section and i want to get field value from last row only , below my code ... could help please


 


NWF$(".repeatedSection .nf-repeater-row:not('.nf-repeater-row-hidden')").each(function ()


  {


     var row = NWF$(this);


      var YearVal = row.find(".Year option:selected");


      var ActualVal = row.find(".Actual input");


      var TargetVal = row.find(".Target input");


 


     var actual2324 = ActualValue[ActualValue.length - 1];
     var target2324 = TargetlValue[TargetlValue.length - 1];


 

Badge +2

value is undefined, Test it before you post please

Reply