function updateFileName2(inputId, fileName) { const inputFile = $('#' + inputId); const nameFileSpan = inputFile.next('label').find('.nameFile'); nameFileSpan.text(fileName); } @foreach($tempFiles as $element) @php $oldVal = old('temp_' . $element); @endphp @if($oldVal) $('#temp_{{ $element }}').val({!! json_encode($oldVal) !!}); (function(){ var tempVal = $('#temp_{{ $element }}').val(); console.log('temp val for {{ $element }}:', tempVal); if(!tempVal) return; var firstUnd = tempVal.indexOf('_'); var nameWithExt = firstUnd !== -1 ? tempVal.substring(firstUnd + 1) : tempVal; var lastDot = nameWithExt.lastIndexOf('.'); var display = lastDot !== -1 ? nameWithExt.substring(0, lastDot) : nameWithExt; console.log('restored temp id:', tempVal, ' -> display:', display); updateFileName2('{{ $element }}', display); })(); @endif @endforeach