if (typeof(document.getElementById('id_photo')) != 'undefined' && document.getElementById('id_photo') != null) { var input = document.getElementById('id_photo'); input.fname = document.getElementById('filename'); input.addEventListener('change', function(e){ e.target.fname.innerHTML = e.target.value.split("\\")[2]; }); }