Javascript send text function

Javascript send text function

Simple send text function that you could use for front end tests within a browser. Replacing the value is quite easy with standard Javascript, however the DOM does not realize that the value has changed. For instance lets say you had an input field that is required to be filled in. If you just change the value by its self it will never remove the required message. To complete this we fire off a change event.

function sendText(id, val) {
  //find the element
  var el = document.getElementById(id);
  
  //replace the value of the element with val
  el.value = val;
  
  //create an event to let the DOM know something changed.
  var event = new Event('change');
  
  //fire off the event
  el.dispatchEvent(event);
}