Capture Filename from Single File Upload Field

Experimental Snippet 🧪

Instructions

Code

Filename: gw-capture-filename.js

/**
 * Gravity Wiz // Gravity Forms // Capture Filename from Single File Upload Field
 * https://gravitywiz.com/
 *
 * Experimental Snippet 🧪
 *
 * Instruction Video: https://www.loom.com/share/294728eabe244fe8aba72b051155b4d7
 *
 *  * Installation:
 *    1. Install and Activate https://gravitywiz.com/gravity-forms-code-chest/
 *    2. Navigate to Form Settings > Custom JavaScript and add this snippet.
 */
var uploadFieldId = 4;
var targetFieldId = 5;
var template = '{filename}';

var $uploadField = $( '#input_GFFORMID_{0}'.gformFormat( uploadFieldId ) );
var $targetField = $( '#input_GFFORMID_{0}'.gformFormat( targetFieldId ) );

$uploadField.on( 'change', function() {
	var filename = $( this ).val().split("\\").pop();
	$targetField.val( template.replace( '{filename}', filename ) ).change();
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.