0

I have some form fields like <input>, <textarea>, <dropdown> etc.

If a user make a change in any form field it should be prompted at once that field is changed.

I applied different events like onKeyPress (did not work for backspace key), onChange (works when lose focus).

Is its not possible in simple/plain JavaScript then I want to know it in Dojo.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190

3 Answers3

1

see stack question: Detect all changes to a <input type="text"> (immediately) using JQuery

while the accepted answer is using jquery setInterval works just fine with javascript. just use GetElementByID or GetElementsByTagName to get your input fields and then just check their values against themselves.

Community
  • 1
  • 1
Ryan
  • 5,644
  • 3
  • 38
  • 66
  • 1
    Noting that the requirement to know "straight away" is unreasonable in most circumstances. – RobG Aug 23 '11 at 06:56
0

There is no HTML "dropdown" element, you probably meant a select.

For some controls you can compare a control's current value to its defaultValue to see if it's changed, pick whatever event suits for the control. For select, onchange is best and compare to the previously selected option (if there was one). Input and textarea you can likely use keyup. Radio buttons and checkboxes click and compare with the one that was checked previously (if there was one).

RobG
  • 142,382
  • 31
  • 172
  • 209
  • For Input and textarea onKeup fails when I press only ctrl key – Muhammad Imran Tariq Aug 23 '11 at 06:27
  • 1
    You can't change the value by only pressing the control key so if it doesn't dispatch an event, why do you care? The control, shift and option keys (among others) are modifier keys that form part of a key event from other keys that may change the control's value. – RobG Aug 23 '11 at 06:53
  • But the input should still have the same value. Just make a check for that to eliminate such events (shift, alt .. etc) – Ibrahim AshShohail Aug 23 '11 at 07:33
0

Through javascript attach event onFocus on input field. Onfocus setTimeout() and make it recursive. If any content will change, it will notify. In my case it works; even I did another task from it. I save existing input field value on page load. e.g it was abc on page load. User change it to abcd then there will be a message for unsave changes. But when user delete d and make it abc again then message will disappear, means there are no unsaved changes.

Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190