Harrington Web

Javascript send text function

Thursday, September 29, 2016
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);
}

1 comment :

  1. That is a good tip particularly to those fresh to the blogosphere. Short but very precise information… Thank you for sharing this one. A must read post! gmail login email

    ReplyDelete