/    Sign up×
Community /Pin to ProfileBookmark

Prototype and input text

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?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisAug 04.2006 — This contains some guesswork, but hopefully it will get you somewhere:
<i>
</i>var stuff = document.getElementsByTagName("INPUT")
for (var i=0; i&lt;=stuff.length; i++)
{if (stuff[i].type == "TEXT")
{stuff[i].setFocus = function () { // whatever}
}
×

Success!

Help @Frisbee spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...