0

How to format input text on html, sample input: Hi hello

I like to display the input like this

'Hi','hello',

When I hit enter, single quote with a comma will automatically display.

Any suggestion? Thank you.

3 Answers 3

1

The text is then formatted and returned to the input field. You only need an eventlistener, a function that converts the text.

const input = document.getElementById('watch');
input.addEventListener('keypress', function (e) {
    e.preventDefault();
    if (e.key === 'Enter') {
      input.value = input.value.split(' ').map(s => `'${s}'`).toString() + ',';
    }
  return false;
});
<form>
  <input type="text" value="Hi World" id="watch">  
</form>

Sign up to request clarification or add additional context in comments.

Comments

1

You can use split and join

const str = "Hi hello";
let output = '';
if(str)
     output = `'${str.split(" ").join("','")}',`;
 console.log(str);

Comments

0

const string = 'a b c'
console.log(string.split(' ').map(str => `'${str}'`).toString() + ',')

Comments

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.