In our last article we left off with a glimpse of JavaScript Events. In this tutorial we are going to go through each one and learn how to use them to create more dynamic web sites. So slap on your seat belts and get your helmets ready. This is gonna be an action-packed episode.
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>