Following is the simple jQuery function but I couldn’t figure it out.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Screen Class</title>
<script type="text/javascript" language="javascript">
//<![CDATA[
function getFocus()
{
document.getElementById("txtScreenClassCode").focus();
}
$(document).ready(function()
{
$("#txtScreenClassCode").keypress(function(event)
{
if (event.keyCode==46 || event.keyCode==8|| event.keyCode==9 || event.keyCode>=35 && event.keyCode<=40)
{
// let it happen, don't do anything
}
else
{
if (event.charCode<48 || event.charCode>57 || event.charCode==16)
{
event.preventDefault();
}
}
});
});
//]]>
</script>
</head>
<body onload="getFocus();">
<input type="text" id="txtScreenClassCode"
name="txtScreenClassCode"style="background-color: white">
</body>
</html>
In the above code the jQuery function $(document).ready(function() doesn’t work. It is meant to allow only digits in the TextField mentioned. The browser shows an error at the beginning of this function i.e on this line itself $(document).ready(function() that indicates that Object expected. What should be wrong with this function? I have teated it with IE and Mozilla Firefox and I also tried to put it into a different directory and enclosed it within the CDATA tag but not to avail.
You need to include jQuery library on the page.
If you don’t have jQuery in your application you can include it from one of the jQuery CDN’s mentioned here http://www.jquerycdn.net
If you don’t know how to include jQuery just add this
scripttag inside theheadsection of your page markup.This will include jQuery 1.7 minified js file from
Google Ajax API CDN