0

I want to trigger a function when one of my field (textbox) changed. The problem is: the content of that textbox is filed automatically by an external script(That I don't have access to), therefore the onChange listener won't trigger.

I've tried a lot of code, but here's the two closest (I think?) result I have:

Here's the script and the field

<script async src="SomeNiceScriptHere.js"></script>
<input type="text" name="wt_embed_output" id="wt_embed_output" class="wt_embed_output"/>

Code 1:

function myFunction() {
    var x = document.getElementById("wt_embed_output").value;
    alert("The input value has changed. The new value is: " + x);
}
    document.getElementById("wt_embed_output").addEventListener("change", myFunction);

Code 2:

$( "#wt_embed_output" ).change(function() {
  alert( "Value has been changed" );
});

For now, it doesn't work when the value is updated with the script, it only works when the user manually changes the value. Does anyone know a way to do it?

mr_info
  • 171
  • 1
  • 7
  • Do you have control over the external script? If so, make it call the change function after changing the value. If not, use `setInterval` to keep comparing the current value to the previous one, and call the function if they differ. –  May 16 '19 at 18:36
  • No I don't. I've updated the post! – mr_info May 16 '19 at 18:37
  • @RobbieMilejczak even with `document.getElementById("wt_embed_output").addEventListener("input", myFunction);` the code won't trigger – mr_info May 16 '19 at 18:39
  • Have you tried input event as `$("#wt_embed_output").on('input', function(){ console.log("Your code here"); });` – Rohit Mittal May 16 '19 at 18:40
  • This might help: https://stackoverflow.com/questions/42427606/event-when-input-value-is-changed-by-javascript – Robbie Milejczak May 16 '19 at 18:48

3 Answers3

0

Try adding event listener in the input.

var el = document.getElementById('wt_embed_output');
el.value = 'New Value'
el.dispatchEvent(new Event('oninput'));
  
document.getElementById("wt_embed_output").addEventListener("oninput", myFunction());


function myFunction() {
  var x = document.getElementById("wt_embed_output").value;
  alert("The input value has changed. The new value is: " + x);
}
<input type="text" name="wt_embed_output" id="wt_embed_output" class="wt_embed_output" />
Anand G
  • 3,130
  • 1
  • 22
  • 28
  • By adding: `onChange="myFunction()"` it still won't work, but you use `el.dispatchEvent(new Event('change'));`. Was I supose to add this code somewhere? – mr_info May 16 '19 at 18:57
  • Edited my Answer, check now – Anand G May 16 '19 at 19:11
  • this is triggered when the page load, but my script run when the user clicks a button. So technically it's working, but not for my usecase. – mr_info May 22 '19 at 13:15
0

onchange only occurs when the change to the input element is committed by the user, most of the time this is when the element loses focus.

try using oninput event;

if that doesn't work you can use a solution that works for all cases: set up a timing event using setInterval()

saurabh
  • 2,553
  • 2
  • 21
  • 28
  • It doesn't work, even with the `document.getElementById("wt_embed_output").value= "changes value";` I'm also trying by changing the value into the console. Do you think this cause an issue? – mr_info May 16 '19 at 19:03
0

This isn't a perfect solution, but it works. Since I wasn't able to fetch the text in the field right after the script execute, I did a little workaround with myFunction():

<script>
    function myFunction() {
        if(document.getElementsByClassName('wt_embed__message')[0].getElementsByClassName('main')[0].textContent != 'Upload completed!'){
            setTimeout(myFunction, 500);
        }
        else{
            var wt_output = document.getElementById("wt_embed_output").value;
            //do something
        }
    }
    window.addEventListener("load", myFunction);
</script>

-> When the page load, my function is called until the external scripts runs. Once I get a specified message, I know the script is done so I get out of the 'if' condition.

mr_info
  • 171
  • 1
  • 7