onChange for a textfield

Posted on 2006-07-21
Last Modified: 2011-10-03
Hi guys,

I want to know how I can make an alert, which will display the contents of a textfield, after the textfield's value has changed. I want this message to run only if:

- The contents of the field has changed from the original value.
- The field is no longer selected (eg, tab was pressed, or something else was clicked)

I only need this to work with Internet Explorer, any ideas guys?

Question by:Cyber-Drugs
  • 3
LVL 23

Accepted Solution

apresto earned 300 total points
ID: 17153664
Hi Cyber-Drugs,

var oldValue = "";

function alertOnChange( txtBox )
   if ( txtBox.value != oldValue )
       oldValue = txtBox.value;
       alert( oldValue );

onchange="alertOnChange( this );"

LVL 23

Expert Comment

ID: 17153702
sorry, stick it in the onBlur event
LVL 19

Assisted Solution

nschafer earned 200 total points
ID: 17153709
Hi Cyber-Drugs,

<script type="text/javascript">
var txtVal
function setVal() {
  txtVal = document.getElementById('YourTextBoxId').value

function checkVal() {
  if (document.getElementById('YourTextBoxId').value != txtVal) {
    alert('You changed the value.');

In your text box have code like this:

<input type="text" id="YourTextBoxId" onfocus="setVal();" onblur="checkVal();" />

Hope this helps,

Author Comment

ID: 17153755
Thanks for the code guys, I am going to use apresto's code as it runs with a single function, and call, but both answers are valid, so I am going to split the points.

Thanks again guys!
LVL 23

Expert Comment

ID: 17153802
No Problem, Glad i could help


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now