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

Discussion

Discussion

Javascript - Reading from a field in a collection works outside of the collection but not inside

posted on March 21, 2018

Even though the preview makes it appear that the class name for the field inside of the collection is not being carried across as the user adds new sets to the collection, I can cycle through all the fields on the form using this.

$('.each input').each(function () {
          
            s += parseNumber($(this).val());

        });

However if I try to stay within the collection object by class name "col" it only returns the first "each" field no matter how many sets are in the collection. Even though it registers change events for the other fields.

  $('li.col').on('change', function(){
    $('li.col').each(function(){
    alert($(this).find('.each input').val());
    });
  });

For example if I put 100 into the first "each" field. I get an alert for 100. If I put 200 into the second "each" field, I get an alert for 100 again. No mention of the 200.

0 0
replied on April 4, 2018 Show version history

So far I have found that adding a "ul" to my collection class allows me to work within each row on the collection. However it is super unreliable.

 

For example

This code simply says, if a change has been made in my collection, for each item, find my input and alert the result. If I have 1 item in my collection, it works returning the value of my input. If I have 2 items in my collection it returns 5 values. I don't even have 5 fields with that class name, who knows what it is finding. For example if I enter 1 for the first item, and 2 for the second item, i get the following output: 1,2,undefined,1,2

Javascript defies the rule that computers do exactly what their told.


//Wait for page to load
$(document).ready(function () {  
 
  $('li.myCol').on('change', function() {
     $('li.myCol ul').each(function () {
         
  
          var start = $(this).find('.myInput input').val(); 
          alert(start);
  
            
            });
      });
  

});

 

New find: There is this strange code appearing out of nowhere when I add additional items to my collection. I think this might be the culprit because it is also using the ul tag. What in the world is an awesomplete child?

<ul hidden="" class="awesomplete-child cf-medium"></ul>

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

Sign in to reply to this post.