Extend Form Element Methods

Posted on 2010-04-04
Medium Priority
Last Modified: 2012-05-09
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!
Question by:js_vaughan
LVL 11

Expert Comment

ID: 29761069
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);

Accepted Solution

yogi4life earned 2000 total points
ID: 29777311
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

LVL 10

Author Closing Comment

ID: 32820115
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!

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question