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

Question

Question

Get filename from upload button

asked on April 7, 2022
I have this form I need to get the value of the label below the file upload button and put that text in the last column on the right

I am trying with this code but it only works with the first line
How can I get the file name from each row of the table?

 

$(document).ready(function () {  
  
      $('#q5 .fileuploader').on('change', function() {      
          var $row = $(this).closest('tr');
          $row.find('td').eq(4).text("");
        var filename = $row.closest("tr").find('.fileuploader')[0].value.split('\\').pop();
        alert(filename.substr(0, filename.length-4));      
        var nombre = filename.substr(0, filename.length-4);  
        $row.find('td').eq(4).val(nombre);      
    });

});

 

0 0

Replies

replied on April 7, 2022
Thank you very much for the answer, it worked correctly at the first try, I had been looking for the solution for two weeks, I do not have much experience in java and I urgently needed to solve this problem
1 0
replied on April 8, 2022

You are very welcome.

You're definitely not the first person that has tripped up.

Please consider marking the post as answered, and have a wonderful day!

0 0
replied on April 7, 2022

Your problem is likely because the code that is running to create your change event listener is being run as soon as the document is loaded.  I'm guessing you have your table set-up so that it starts with one row.  When you add additional rows, the code isn't being re-run to add the change event listener to the new rows, so they don't trigger the same code when changes happen.  You could test this by tweaking your table on the Layout page to start with more rows, and see if you code works on those other rows.

I have not fully tested this, but some changes to your code like this, will likely work.  This runs your same code at two different times - it does this by putting your code into a custom function and then calling that function multiple times - it calls it when the form is first loaded, and then calls it again any time you add a new row to the table.

$(document).ready(function () {  
  
  //Run the function to add the event listener when the form is loaded:
  AddMyListener();
  
  //Run the function to add the event listener when the table is changed:
  $('.cf-table-add-row').on('click', AddMyListener);
  
  //This is the function to add the event listener:
  function AddMyListener()
  {
    $('#q5 .fileuploader').on('change', function() {      
      var $row = $(this).closest('tr');
      $row.find('td').eq(4).text("");
      var filename = $row.closest("tr").find('.fileuploader')[0].value.split('\\').pop();
      alert(filename.substr(0, filename.length-4));      
      var nombre = filename.substr(0, filename.length-4);  
      $row.find('td').eq(4).val(nombre);      
    });
  }

});

 

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

Sign in to reply to this post.