7

How to detect the changes in input values when it is dynamically changed. as the change on txt2 clear the value in txt1 . I need to detect that it has cleared or changed.

Onchange does not do that.

<input id="txt1" type="text" onchange="SetDefault();" onpaste="this.onchange();" oninput="this.onchange();"> <br> <input id="txt2" type="text" onchange="SetDefaultSecond();" onkeyup="this.onchange();" onpaste="this.onchange();" oninput="this.onchange();"> <script> function SetDefault(){ alert("Change1"); } function SetDefaultSecond(){ $("#txt1").val(''); alert("Change2"); } </script> 
9
  • I checked your code, typing in txt2 will erase txt1 but you need to use the jquery script(if you didnt invoke it) <script src="ajax.googleapis.com/ajax/libs/jquery/1.12.4/…>,, Commented Sep 7, 2016 at 16:55
  • onchange is not fired when changed with JavaScript. You need to fire the event manually. Commented Sep 7, 2016 at 16:57
  • @odai I need to trigger that there is a change in value happened in txt1 Commented Sep 7, 2016 at 17:07
  • 1
    Possible duplicate of Detect all changes to a <input type="text"> (immediately) using JQuery Commented Sep 7, 2016 at 17:21
  • 2
    There's no event for what you need (except onpropertychange in IEs, not useful in general though). Even Mutation Observer can't be used (can detect attribute changes of an input only). Hence the only way left is to use a timed function to check, if a value has been changed. Commented Sep 7, 2016 at 17:32

5 Answers 5

7

You can manually trigger the input event when you clear the first input like this:

<input id="txt1" type="text"> <br> <input id="txt2" type="text"> <script> $("#txt1").on("input", function() { alert("Change1"); }); $("#txt2").on("input", function() { $("#txt1").val('').trigger('input'); alert("Change2"); }); </script> 

jsFiddle here: https://jsfiddle.net/dr0oabj1/

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

5 Comments

It works but only for if it is empty val('') , I search for a way to detect the change in general as if the value is 4 and changed to be 6 ..etc
@YosraNagati It works by emptying the value and then triggering a change. You can change the val('') to whatever you would want. Check out the answer here for a snippet to detect any and all input changes without triggering the event manually: stackoverflow.com/questions/1948332/…
Actually this field on my project is changed from different sources dynamically , and I want to detect when it get changed.
@YosraNagati Then the link in my last comment should be exactly what you're looking for.
@YosraNagati now i get the point but it was not so clear from the question. in that case you need to use setInterval technique described in Kodie's link.
3

you need to use oninput event to capture any change to an input. it triggers every time the value changes due to typing/pasting etc. difference between onchange & oninput is onchange only triggers after focus goes off the input where as oninput triggers every time values changes due to any reason.

function SetDefault(){ alert("Change1"); } function SetDefaultSecond(){ $("#txt1").val(''); alert("Change2"); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <input id="txt1" type="text" oninput="SetDefault();" /> <br> <input id="txt2" type="text" oninput="SetDefaultSecond();"/>

3 Comments

i see, you want the alert for txt1 when it's cleared by txt2 change. 1min plz
when it is changed generally , cleared or changed to another value
oninput does not detect with val() is changed.
2

Try this :

$(document).ready(function(){ $("#txt2").on("input",function(){ $("#txt1").val(''); console.log("Change2"); }) $("#txt1").on("input",function(){ console.log("Change1"); }) }) 

Final code :

<!DOCTYPE html> <html lang="en"> <head> </head> <body> Text 1 : <input id="txt1" type="text"> <br><br> Text 2 : <input id="txt2" type="text"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function(){ $("#txt2").on("input",function(){ $("#txt1").val(''); console.log("Change2"); }) $("#txt1").on("input",function(){ console.log("Change1"); }) }) </script> </body> </html>

Comments

1

Kodies answer is right; just try this ultra simple example and then proceed to Detect all changes to a (immediately) using JQuery

<input id="txt1" type="text" oninput="console.log('input')" onchange="this.oninput()"> <br> <button onclick="eval(this.textContent)">console.log('clearing'); txt1.value=''</button> <br> <button onclick="eval(this.textContent)">console.log('clearing <b>and triggering</b>'); txt1.value=''; <b>txt1.onchange()</b></button>

Comments

0

use this in txt2

 oninput="changeClass(txt2);" 

and in script

function changeClass(text) { if(text.value==''){ //code for clear txt1 } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.