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

Question

Question

Javascript to loop through each row of the table is not working

asked on October 22, 2020

I have a table in my form with 2 columns which is populated from lookup rules. I am just trying to read the values of each column in the table using JavaScript but it doesn't execute any code inside the foreach loop.  I am not sure what is wrong with my JavaScript.

 

 

$(document).ready (function () {

  $('.cf-table-block').on('change lookup', 'input', tablevalues);
 
   function tablevalues() { 
      
    $('.cf-table-block tbody tr').each(function () {    
     
       var name = $(this).find('.name input').val();
       var link = $(this).find('.link input').val();  
      
    });
    }
})

 

 

0 0

Replies

replied on October 22, 2020

How do you know that the code in the .each method is not executing? With Debug? So you have confirmed that your function is running and only the code in the .each is not executing?

0 0
replied on October 22, 2020 Show version history

Yes when I debug, it is just coming directly out of the loop without entering inside. I am sure the rows are populated with values from lookup rule

0 0
replied on October 22, 2020

It looks right to me, console.log($('.cf-table-block tbody tr')); to see if you get anything

0 0
replied on October 23, 2020 Show version history

Edit: Sorry I changed the input  class names when I was testing locally. Make sure they match yours.

Try the following: 

$(document).on('lookupcomplete', function(event) {
  tableValues();
});

function tableValues() {
  $('.cf-table-block tbody tr').each(function() {
    console.log($(this));
    var name = $(this).find('.name input').val();
    var link = $(this).find('.link input').val();
    console.log('name',name);
    console.log('link',link);
  });
}

 

0 0
replied on October 23, 2020

I think something wrong in the debugger, because the code is actually running fine but when i debug it doesn't go through the loop.

 

Thank you all

0 0
replied on October 23, 2020

You're probably already aware, but if you choose to use your original code, the loop will run once for each input that experiences a change event once the lookup completes. If you want to avoid this then run it only after its been populated with data then you'll want to run your tableValues() function on a lookupcomplete event (as shown in my code example). Otherwise what you have does work as expected.

 

 

0 0
replied on November 2, 2020 Show version history

Hi Vineela,

 

I tried your code and to check if it's working, just do a simple alert.

This one doesn't work.

$(document).on('lookupcomplete', function(event) {
  tableValues();
});

function tableValues() {
  alert("test");
}

That means your function doesn't called.

Try this

$(document).on('onloadlookupfinished', function(event)
	{
  		tableValues();
	});


function tableValues() {
  alert("test");
}

 

Next, change "alert("test");" by your code.

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

Sign in to reply to this post.