Copy and Modify Input value to another Input, JavaScript

1

I have a question about a code that is killing me ...

What I need is that I copy the input value in another input, but that it is different, that is to say without spaces and in minuscule everything, example:

Input 1: Hello World Input 2: helloworld (or hello_world )

To copy what is written in Input 1 in Input 2 but in lowercase, without spaces or with a hyphen under

The code I have is the following:

CODE JS:

document.getElementById("name").addEventListener('keyup', autoCompleteNew);

function autoCompleteNew(e) {

    $("#name").keyup(autoCompleteNew); 
    var value = $(this).val(); $(this).val = value.toLowerCase().replace(" ", ""); $("#short-name").val(value);

}

I thank you for a world, your help with this.

    
asked by ortizalfano 29.08.2018 в 19:11
source

1 answer

2

To eliminate spaces, you need a regular expression. To convert into minuscules you use .toLowerCase() . It would stay like this:

document.getElementById("name").addEventListener('keyup', autoCompleteNew);

function autoCompleteNew(e) {            
    var value = $(this).val();         
    $("#short-name").val(value.replace(/\s/g, '').toLowerCase()); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="name" type="text">
<input id="short-name" type="text">
    
answered by 29.08.2018 / 19:20
source