2

I need to fill in the empty spaces of the name of a file by selecting it with an <input type = "file"> using javascript or jQuery.

I tried like that, but it did not work

$(document).ready(function (e){

  $('#file').on('change', function () {
    var cadena = $('#file').val();
    cadena= cadena.replace(/\s/g,"_");
    document.getElementById('file').innerHTML = cadena;

 }
}

1 Answers1

4

If you had run your code and looked into the console, you would have seen that there were parsing errors, because you were missing two closing brackets and semicolons. Once the code is working, the space replacement is working as well:

$(document).ready(function (e){

  $('#file').on('change', function () {
    var cadena = $('#file').val();
    cadena= cadena.replace(/\s/g,"_");
    document.write(cadena);
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="file" />
Constantin Groß
  • 9,010
  • 4
  • 17
  • 44
  • thank you !, this works, but I need the input value to have the new name with full spaces, some idea of ​​how to do it? – Alvaro Vergara Oct 30 '17 at 13:34
  • "new name with full spaces" does not make any sense - the new name has the spaces replaced, so how can it have "full spaces"? – Constantin Groß Oct 30 '17 at 13:38
  • Also, this doesn't make any sense. You don't know whether the client has a file with the new name at all, and for security reasons you can't set a file input to any file you like. – Constantin Groß Oct 30 '17 at 13:38
  • aps, ok, i understand, i need to change the value for take it with php, but i think that i know how to do it, thank you so much for your help! – Alvaro Vergara Oct 30 '17 at 13:40
  • Yes, changing the name on the server-side after uploading but before storing it in its final destination is absolutely possible! :) – Constantin Groß Oct 30 '17 at 13:41