0

Using this simple code when you fill #input1 field, the #input2 input gets autofilled with same value:

$("#input1").keyup(function(){
    $("#input2").val(this.value);
});

So e.g. if you type in 'Foo Bar' into #input1, it also gets filled as 'Foo Bar' in #input2.

But how do I autofill it into #input2 and at same time e.g. convert to all lower case or all uppercase and with no spaces? So e.g. if #input1 contains 'Foo Bar' then #input2 autofills with 'FOOBAR' or 'foobar'?

http://jsfiddle.net/bxHQ5/1037/

3 Answers 3

1

You can do something like this:

$("#input2").val($("#input1").value.toLocaleLowerCase().replace(/\s/g, ''));
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfect. Cheers.
1

Like this:

$("#input1").keyup(function(){
    $("#input2").val(this.value.toLocaleLowerCase());
});

Or for uppercase, like this:

$("#input1").keyup(function(){
    $("#input2").val(this.value.toLocaleUpperCase());
});

4 Comments

replace(' ', '')
You can check out all the available functions on the string type here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
The replace(' ', '') replace only first space.
Sorry about that, try replace(/ /g, '')
1

You can call the function ToLowerCase and toUpperCase and replaceto quit the spaces, something like this:

$("#input2").val(this.value.toLowerCase().replace(' ', ''));

2 Comments

This replace only first space, ignores any other multiple spaces.
@user3108268 try the Vlad Wandimirkin's solution it matchs all spaces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.