Monday, April 30, 2007

Magic of JQuery

We know that in IE, a form gets submitted whenever we press 'Enter' in a text-field. To disable it would require trapping the event in Javascript and disabling the default behaviour.

<input onkeydown="if (event.keyCode == 13){returnfalse;}" name="email">

But to do this in all text fields across hundreds of html pages would be a impossible task. Then suddenly I got the idea of using JQuery's powerful selector concept to apply this to all textfields across all pages. I just needed to add the following lines to the JS file that was present in all pages.

<script src="jquery-1.1.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input[@type=text]").keypress( function() {
if (event.keyCode == 13){return false;}
} );
});
</script>