0

I can't find a solution for this in anywhere. Can someone help me ?

<input type="text" id="myText" oninput="alert(this.value);"><br><br>
<div onclick="document.getElementById('myText').value='123';NowCauseTheEvent,Plz !">
   change
</div>

Basically I need to know how to programatically raise the event input after I change the value programatically as well. (something to place on the "NowCauseTheEvent,Plz" after the document.getElementById('myText').value='123'

ty !

BTW, this is not about custom event, but native event and I will REALLY appreciate to stop to vote for close this one because THIS IS NOT A CUSTOM EVENT !!!!

cweiske
  • 30,033
  • 14
  • 133
  • 194
Marco Jr
  • 6,496
  • 11
  • 47
  • 86
  • Please explain better what you are trying to achieve – Starfish May 20 '16 at 11:55
  • Any reason you are running javascript inline rather than in a function? Also can you try explain what you want the client to do and what you want the webpage to do, I'm finding it hard to understand your question... – NewToJS May 20 '16 at 11:55
  • 1
    Possible duplicate of [How to trigger event in JavaScript?](http://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript) – Justinas May 20 '16 at 11:56
  • Nope. it's not a duplicated. That thing is for custom event. And this is not a custom event. It's a native event., Justinas. – Marco Jr May 20 '16 at 11:57
  • @NewToJs, I cannot use a function. Long story ! Starts with : I need to send raw html to a 3rd party library. – Marco Jr May 20 '16 at 11:58
  • He want's the `oninput` event to fire on his `` when the div's `onclick` inserts `123` into the ``. You could try setting the `onclick` of the `` and then calling `document.getElementById("myText").click()` in the `
    `.
    – jdgregson May 20 '16 at 11:59
  • Well you need to give more information other than **"Something"** - *"something to place on the "NowCauseTheEvent,Plz""* – NewToJS May 20 '16 at 11:59
  • If you read the code , you will understand, NewToJs – Marco Jr May 20 '16 at 12:04
  • I can't jdgregson ...must be very very very mandatory on the input event - the 3rd party library requires it. – Marco Jr May 20 '16 at 12:05
  • @MarcoS.Junior it works in my answer below, but I removed it as calling `oninput` is a similar but far better idea. – jdgregson May 20 '16 at 12:12

2 Answers2

2

This will work across browsers and trigger the event you require.

<div onclick="document.getElementById('myText').value='123';document.getElementById('myText').oninput()">
    change
</div>

Use the following in your placeholder:

document.getElementById('myText').oninput()
1

This works for me on Firefox and Chrome but fails on IE11.

<input type="text" id="myText" oninput="alert(this.value);"><br><br>
<div onclick="document.getElementById('myText').value='123'; document.getElementById('myText').dispatchEvent(new Event('input'));">
       change
</div>
michaPau
  • 1,598
  • 18
  • 24
  • Awesome !! That's exactly what I need ! I just need to find a way to make this work in IE...I will try to find a new one to make it work and Ie and then I send you back to edit and you got my "answered" – Marco Jr May 20 '16 at 12:12