cara menghapus ruang di bidang input html

let input = document.getElementsByName("my_input")[0];
let val = input.value.replace(/\s/g, "");
alert(val);
Undefined