I am having a textarea and a button. There are two classes buttonEnabled and buttonDisabled. I want to apply buttonEnabled only when there is some text in the textarea. So if user is typing in a textarea then button looks enabled and when he clears all the text either by selecting and cutting it or by backspace. I want my button to look disabled. Also i want to do this only with javascript and for some reason dont want to use jQuery.
Share
At the moment, the only way you can be sure to pick up every change in input contents straight away is to poll for it. This is because there are many possible ways an input can get edited without generating a
keyupor immediatechangeevent. As well as cut-and-paste there’s drag-and-drop, undo/redo, autocomplete, spellchecker changes…HTML5 proposes an
inputevent that will fire in all cases like this, but browser support isn’t there yet, unfortunately.You can of course back up the polling with quicker checking for the common case where you do get an event. For example: