Pure Javascript listen to input value change

Javascript

Javascript Problem Overview


Is there any way I can create a constant function that listens to an input, so when that input value changes, something is triggered immediately?

I am looking for something using pure javascript, no plugins, no frameworks and I can't edit the HTML.

Something, for example:

When I change the value in the input MyObject, this function runs.

Any help?

Javascript Solutions


Solution 1 - Javascript

This is what events are for.

HTMLInputElementObject.addEventListener('input', function (evt) {
    something(this.value);
});

Solution 2 - Javascript

As a basic example...

HTML:

<input type="text" name="Thing" value="" />

Script:

/* event listener */
document.getElementsByName("Thing")[0].addEventListener('change', doThing);

/* function */
function doThing(){
   alert('Horray! Someone wrote "' + this.value + '"!');
}

Here's a fiddle: http://jsfiddle.net/Niffler/514gg4tk/

Solution 3 - Javascript

Actually, the ticked answer is exactly right, but the answer can be in ES6 shape:

HTMLInputElementObject.oninput = () => {
  console.log('run'); // Do something
}

Or can be written like below:

HTMLInputElementObject.addEventListener('input', (evt) => {
  console.log('run'); // Do something
});

Solution 4 - Javascript

Default usage

el.addEventListener('input', function () {
    fn();
});

But, if you want to fire event when you change inputs value manualy via JS you should use custom event(any name, like 'myEvent' \ 'ev' etc.) IF you need to listen forms 'change' or 'input' event and you change inputs value via JS - you can name your custom event 'change' \ 'input' and it will work too.

var event = new Event('input');
el.addEventListener('input', function () { 
  fn();
});
form.addEventListener('input', function () { 
  anotherFn();
});


el.value = 'something';
el.dispatchEvent(event);

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events

Solution 5 - Javascript

Another approach in 2021 could be using document.querySelector():

const myInput = document.querySelector('input[name="exampleInput"]');

myInput.addEventListener("change", (e) => {
  // here we do something
});

Solution 6 - Javascript

Keydown, keyup, input are events that fire immediately when input changes, I would use keydown or input events to get the changed value from the input box.

const myObject = document.getElementById('Your_element_id');
myObject.addEventListener('keydown', function (evt) {
// your code goes here 
 console.log(myObject.value);
});

Solution 7 - Javascript

assign will not trigger any of existing listeners change, keyup, keydown, input, I usually use onkeyup, whenever I assign, I call the method to trigger the function.

adder.onclick = () => {
    let val = parseInt(input_el.value);
    if(isNaN(val) || val + 1 < 0){
        input_el.value = 0;
    }else{
        input_el.value = val + 1;
    }
    input_el.onkeyup();
}
reducer.onclick = () => {
    let val = parseInt(input_el.value);
    if(isNaN(val) || val - 1 < 0){
        input_el.value = 0;
    }else{
        input_el.value = val - 1;
    }
    input_el.onkeyup();
}
input_el.onkeyup = () => {
    input.value = input_el.value;
}

Solution 8 - Javascript

Each time a user inputs some value, do something.

var element = document.getElementById('input');
element.addEventListener('input', function() {
 // Do something 
});

Solution 9 - Javascript

If you would like to monitor the changes each time there is a keystroke on the keyboard.

const textarea = document.querySelector(`#string`)
textarea.addEventListener("keydown", (e) =>{
   console.log('test') 
})

Solution 10 - Javascript

instead of id use title to identify your element and write the code as below.

$(document).ready(()=>{
   
 $("input[title='MyObject']").change(()=>{
        console.log("Field has been changed...")
    })  
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongespinhaView Question on Stackoverflow
Solution 1 - JavascriptQuentinView Answer on Stackoverflow
Solution 2 - JavascriptNifflerView Answer on Stackoverflow
Solution 3 - JavascriptAmerllicAView Answer on Stackoverflow
Solution 4 - JavascriptAzune-NKView Answer on Stackoverflow
Solution 5 - JavascriptRunsisView Answer on Stackoverflow
Solution 6 - JavascriptMitesh vaghelaView Answer on Stackoverflow
Solution 7 - JavascriptWeiloryView Answer on Stackoverflow
Solution 8 - JavascriptUserName NameView Answer on Stackoverflow
Solution 9 - JavascriptNisoView Answer on Stackoverflow
Solution 10 - JavascriptkaiView Answer on Stackoverflow