Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Forked from dislokacia/text_after_upload.js
Last active June 22, 2024 20:38
Show Gist options
  • Save Crocoblock/b1e80427627144297c2dcc631bc818ac to your computer and use it in GitHub Desktop.
Save Crocoblock/b1e80427627144297c2dcc631bc818ac to your computer and use it in GitHub Desktop.
JetFormbuilder media field text after upload
document.addEventListener( 'DOMContentLoaded', function() {
const {
addAction,
} = window.JetPlugins.hooks;
const textChoose = 'Choose File';
const textEmpty = 'No file has been chosen';
const textUpload = 'File Uploaded';
function getFileNames( fileList ) {
if ( ! fileList.length ) {
return '';
}
let names = [];
for ( const file of fileList ) {
if ( file.name ) {
names.push( file.name );
}
}
return names.join( ', ' );
}
addAction(
'jet.fb.input.makeReactive',
'random-index/on-observe',
function( input ) {
if ( input?.nodes?.length < 1 ) {
return;
}
if ( ! input.nodes[0].classList.contains( 'jet-form-builder-file-upload__input' ) ) {
return;
}
const $ = jQuery;
const upload = $( input.nodes[0] );
const fields = $( input.nodes[0].closest( '.jet-form-builder-file-upload__fields' ) );
fields.append(`<input type="button" class="addfile" value="${textChoose}"/>`);
fields.append(`<label class="labeladdfile">${textEmpty}</label></div>`);
const labelAdd = fields.find('.labeladdfile');
const buttonAdd = fields.find('.addfile');
$( input.nodes[0] ).css('display','none');
buttonAdd.click(function () {
upload.trigger('click');
});
input.value.watch( function() {
if ( this.current.length ){
buttonAdd.attr( 'value', textUpload );
labelAdd.html( getFileNames( this.current ) || textUpload );
} else {
labelAdd.html( textEmpty );
buttonAdd.attr( 'value', textChoose );
}
} )
}
);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment