In Xojo, HTMLHeader is read-only. So I loaded jQuery in a PageSource I placed on the WebPage.
In the shown event, things did not go quite smoothly.
I added a TextField on the page called txtcmp_code1
as I guess is the name of the control in your first line of code.
When run, the code triggers the following error :
[code]Could not execute returned javascript: $ is not defined
Source: document.getElementsByName(‘txtcmp_code1’)[0].id= ‘txtcc’ ;
$(’#txtcc’).keydown(function(e) { if (e.keyCode == 9 || e.keyCode == 13 || e.keyCode==32 ) { e.preventDefault(); } }) ;
$(’#txtcc’).keypress(function(e) { var allow_key_codes = [8,46,48,49,50,51,52,53,54,55,56,57]; if ( $.inArray(e.keyCode , allow_key_codes) < 0 ) { e.preventDefault(); } }) ;[/code]
From what I understand the first line is OK but the issue is with the following lines.
In case the change of ID in the first line was the cause of the issue because in Xojo ControlID is read only, I tried :
[code] ‘prevent tab,enter, spacebar
ExecuteJavaScript(" $(’#"+txtcmp_code1.ControlID+"’).keydown(function(e) { if (e.keyCode == 9 || e.keyCode == 13 || e.keyCode==32 ) { e.preventDefault(); } }) ; ")
‘only allow delete/backspace and numbers
ExecuteJavaScript(" $(’#"+txtcmp_code1.ControlID+"’).keypress(function(e) { var allow_key_codes = [8,46,48,49,50,51,52,53,54,55,56,57]; if ( $.inArray(e.keyCode , allow_key_codes) < 0 ) { e.preventDefault(); } }) ; ")[/code]
But still, I get the $ is not defined
error.
Finally, I tried to remove the “$” which after all could simply be a prompt, and get :
Could not execute returned javascript: Object #txtcc has no method 'keydown'
Your insight will be appreciated.