Extend Form Element Methods

Hopefully my title was correct, so if there is a "term" for what i am asking, please let me know so I can research it better in the future.

For now, lets assume I have the following function created:
clearField = function(field){
    field.value = '';

If i had a text field object referenced as "myTextField" then i could clear the text field like so:

My question is, how can I create my function so that I may call it like so:

P.S. - I would like to avoid frameworks

Thanks experts!
LVL 10
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, in short you cannot write functions for standard form components. It is not possible for a JavaScript enabled browser to provide functionality that it is not aware of.

The only thing you can do is to create an Object in JavaScript and attach functions to it; but not functions on standard form component like TextBox.

var myObject=new Object();
myObject.add=function(a,b){return a+b};  
// myObject now has a method named "add"
// and you can use it like below
myObject.add(1, 2);
If I understand you correctly - this should be what you are looking for (and the attachment shows how it could be made to attach a function to all your input elements of type text):

    <script type="text/javascript">
        function $( e ) { return document.getElementById( e ) };
        function init() {
            $('myText').clearValue = function () { this.value = '' ; return true }
        window.onload = init;
    <input id="myText" type="text" value="1000" />
    <button onclick="$('myText').clearValue()">Clear...</button>

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
js_vaughanAuthor Commented:
yogi4life, thanks for the attached sample.  It would have been sweet to do this without the looping (so it would automatically be available for new inputs created on the fly through the dom) for now though, I will just have to run an additional command with each newly created input.

At the end of the day it worked though, so thanks for the hard work!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.