Hi. Am trying to extend the textboxes of a form with some new methods and properties, but I would like it to work with all the textboxes in a page without adding any code to it but a reference to an external javascript source file.
I would like to use the prototype to do this, but it doesn’t work the way I was trying.
If for example I want to extend all the strings with new methods, I simply add this js code:
String.prototype.myFunction = function() {…}
What should I write instead of “String” in the case I want to manipulate textboxes? I tried a “alert(typeof(myField))” with one of those textboxes and the type returned is “object”.
So I tried the code
Object.prototype.myFunction = function() {…} but it doesn’t work.
when I try to use the method with myField.myFunction() it says that the object doesn’t support that method.
Also, if “Object” did work, it would work with all objects on a page, while instead I want that code work only with textboxes.
For instance, I’ve got some code in that page which displays an alert depending on a particular event, and soon after sets the focus on a field (for example if you type an unattendend value in that field and leave it, the code displays the alert and then sets the focus back to that field, inviting you to amend the value). Normally I would use the code “myField.focus()” to set the focus but I have noticed that – for reasons I don’t know – if before the call to the method .focus() there is an alert, it fails to set the focus.
So I tried a timed some code like
setTimeout(‘document.getElementById(“‘ + myField.id + ‘”).focus()’, 100)
and it works even if there is the alert.
Now, I would like to avoid to write this code every time and also I like the prototype based coding more than writing a function which would do the job by passing the field as parameter.
I would like something like
myfield.setFocus() as new method, where myfield can be ANY of the textboxes in the page. Any idea about how to proceed?