Get new value from one input to another [duplicate] - javascript

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;
})

Related

Sort string based on user input using JavaScript [duplicate]

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>

How can I loop trough 3 input fields and focus each one on click? Javascript [duplicate]

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()

Fill password field programatically like real user [duplicate]

This question already has answers here:
How can I trigger an onchange event manually? [duplicate]
(2 answers)
How do I programmatically force an onchange event on an input?
(11 answers)
Closed 4 years ago.
I have input field like below
<input type="password" placeholder="Password" autocorrect="off" maxlength="30" autofocus="autofocus" animate="true" label="" rules="[object Object]">
and button like below
<button type="submit" class="button-orange wide">Login <!----></button>
So i am filling password with pure javascript like below
document.querySelectorAll('input[type=password]')[0].value = "12345";
document.querySelectorAll('.button-orange')[0].click();
Problem is the actual website is tracking keydown or something , its always alerting me password is empty also when i focus on input password tab , Password filled getting empty.
How do i fill password like real human typed it.
May be you have to set redOnly property of the field to true after filling it with value by JavaScript
document.querySelectorAll('input[type=password]')[0].value = "12345";
document.querySelectorAll('input[type=password]')[0].readOnly = true;

Get the values of two input range with javascript [duplicate]

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

Check if input field has focus and pass input variable to function [duplicate]

This question already has answers here:
Using jQuery to test if an input has focus
(15 answers)
Closed 7 years ago.
I have an input field which will be updated every x seconds:
<input type="number" name="METER" id="METER.NUM" min="0" max="500" step="0.10" oninput="setMeter(currentValue);" >
Now, I want to check if the input field has focus (user clicked into the field).
If the field has focus:
I will stop updating the field and the user can put some value into the field
Then the value should be passed to a function
This is the javascript code:
if (document.getElementById(array[0]).name == "METER") {
// check if the input field has focus
// stop updating
}
What's wrong with my approch/code? I think one problem is, that the function call by oninput doesn't work. But it works by onClick.
use onfocus = myFunction()
function setMeter() {
console.log('hey, im focused');
// do stuff here
}
<input type="number" name="METER" id="METER.NUM" min="0" max="500" step="0.10" onfocus="setMeter();" >

Categories