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:
clearField(myTextField);

My question is, how can I create my function so that I may call it like so:
myTextField.clearField();

P.S. - I would like to avoid frameworks

Thanks experts!
LVL 10
js_vaughanAsked:
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.

anilallewarCommented:
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);
0
yogi4lifeCommented:
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):

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

Open in new window

DOM-functions2.htm
0

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!
0
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
JavaScript

From novice to tech pro — start learning today.