JavaScript Events - OnChange
(Page 3 of 5 )
If a user changes the value of a field, you may wish to punish them. Or alert them, whatever you think is appropriate. Heck maybe you want them to change it; you can throw them a party for all I care. Whatever it is you want to have happen when a value is changed, here is some code to demonstrate how to do it.
<html>
<head>
<script type="text/javascript">
function popup(x)
{
alert('No don't do that.')
}
</script>
</head>
<body>
Place Your Order: <input type="text" id="food" value="hamburger" onchange="popup(this.id)">
</body>
</html>
This code creates a function and a text box with the default value of hamburger. The caption reads: Place Your Order. If you change the text in the text box, it triggers the function, which causes a pop-up button to appear saying: No don’t do that.
You will note that in the pop-up button text I used ’ to insert an apostrophe. If you forget to do so, the code will not work.
Supporting HTML Tags: <input type="text">, <select>, <textarea>
Supporting Javascript Objects: fileUpload, select, text, textarea
Next: OnClick >>
More JavaScript Articles
More By James Payne