-1

The question is how to sort the letter in alphabetic order based on the input in a HTML input tag, then clicks a button to sort it, after click the button the input will move to a text area and is already sorted when button is click, so that button need to have the insert function and sorting function, now the input can be insert to the textarea but not sorted, thanks. Example of input: andwe output: adenw

i want to define my input as an element, and write onclick="sortstring(element of my input)" in button, but i dont know how to define and dont have a sort function yet.

1

2 Answers 2

1

function sortString() {
  const inputElement = document.getElementById('input');
  const sortResult = inputElement.value.split('').sort().join('');
  inputElement.value = sortResult;
  
}
<input id='input' type="text" value=''/>
<button onclick="sortString()">sort</button>

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

1 Comment

The question asked how to do this with user input and a button that they click on. None of that is in your answer.
1

function sortString(str) {
  document.querySelector('#result').innerHTML = str.split('').sort().join('')
}
<input type='text' onkeyup='sortString(this.value)'>
<p id="result"></p>

1 Comment

Please put the sample code in a stack snippet so it can be executed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.