This question already has answers here:
Sort a string alphabetically using a function
(3 answers)
Change input value onclick button - pure javascript or jQuery
(6 answers)
change value of input field onclick
(5 answers)
Closed 3 months ago.
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.
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>
function sortString(str) {
document.querySelector('#result').innerHTML = str.split('').sort().join('')
}
<input type='text' onkeyup='sortString(this.value)'>
<p id="result"></p>
Related
This question already has answers here:
Javascript Focus Button
(2 answers)
Closed 9 months ago.
I have 3 input fields and a button. When I press the button I want to focus the first one, and when I press again, focus the second one and the same for the third one. But when I reach the third one I want to jump again at the first input and so on. A loop or something. I hope someone can help me
this is a rather simple task that can be performed in many ways I propose one of them:
let counter = 0;
document.querySelector('button').addEventListener('click', () => {
document.querySelectorAll('input')[counter].focus()
counter = (counter === 2) ? 0 : ++counter;
});
<input type='text'>
<input type='text'>
<input type='text'>
<button>press me</button>
Use a counter that changes based on the clicks you've made, and resets to the maximum selected input
Reference:
Document.querySelector() / Document.querySelectorAll()
EventTarget.addEventListener()
This question already has answers here:
How to Copy Value from one Input Field to Another
(2 answers)
Closed 2 years ago.
Let's say I have two inputs in HTML:
<input type="number" id="test1">
<input type="number" id="test2">
Now I've an algorithm which should be called after enter of some number in input. For example, if I enter 10 in first input, in twice input must be returned 5 (10/2) without click of any button. So how to archive that?
You can use the input event listener.
document.getElementById("test1").addEventListener("input", function() {
document.getElementById("test2").value = this.value/2;
})
This question already has answers here:
Sending form text input to console.log for testing
(5 answers)
Closed 3 years ago.
I have the following input element(note, I don't have inside <form> tags:
<input id = 'search' type="text">
And a simple script:
<script type="text/javascript">
let query = document.getElementById("search").value;
console.log(query)
</script>
I saw on many other answers that this was how you can get a value from an input, but the only thing printed to my console is an empty string. Is there a way to get the input on each keystroke? So say that if a user types in 'apple', the following gets printed to the console:
'a',
'ap' ,
'app',
'appl',
'apple'
Thanks for any help.
Yes, add an event handler to the input element.
function handle(event) {
// inside this event handler, "this" is the element that it was attached to
// and the current value of the element can be retrieved with this.value
console.log(this.value);
}
document.getElementById("search").addEventListener("input",handle);
<input id = 'search' type="text">
try with onchange attribute in input
<input id = 'search' type="text" onchange="gettext()">
<script type="text/javascript">
function gettext()
{
let query = document.getElementById("search").value;
console.log(query)
}
</script>
This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 6 years ago.
I have two input type = range:
<input type="range" id="range_1" name="rangeA">
<input type="range" id="range_2" name="rangeB">
I want to get the values and put them in a function.
How can i do that?
first you need to select the input, thankfully you have separate ids on them,
Vanilla JS:
var element = document.getElementById('range1').innerHTML;
Here is a list of stuff you can then do with that element, like innerHTML
https://developer.mozilla.org/en-US/docs/Web/API/Element
This question already has answers here:
Get the value in an input text box
(13 answers)
Closed 8 years ago.
I am very new to programing so sorry if this question is to vague.
I have a simple html input field:
input class="draft" type="text"
and I have a javascript function (the function only accepts strings):
Message.send()
Using javascript/jQuery how do I take the string the user typed into the input field and place it into the Message.send() function (in string form)?
Thanks in advance for any help
jQuery:
Message.send($("input.draft").val());
javascript:
Message.send(document.querySelector("input.draft").value);
No jQuery needed. I'm not sure what you mean by place it into the Message.send() function (in string form), so I assume you get the value in the text input and use it as an argument in the Message.send() function.
Try this:
Message.send(document.getElementsByClassName("draft")[0].value);
Assuming your input field is something like this:
<input type="text" id="someInputField" name="someInputField" class="inputFields" />
You could do this
<script type="text/javascript">
//We use the field's id to refer to it and get to its value (the text in it):
var message = $('#someInputField').val();
//And then you might call the function like:
nameOfTheFuntion(message);
</script>
You would need to have jQuery libraries to make it work though, but you could do without them by replacing:
$('#someInputField').val();
with
document.getElementById('someInputField').val();
Give your <input> box an id for example
<input type="text" id="blah" />
In your javascript you are able to reference the <input> like so:
var strBlah = document.getElementById("blah").value;
Now you have the value typed into the <input> box so you would do the following:
Message.send(strBlah)