Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6319127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:45:03+00:00 2026-05-24T15:45:03+00:00

How can one assign a Javascript namespace to an HTML element and call functions

  • 0

How can one assign a Javascript namespace to an HTML element and call functions defined in said namespace on this element?

I asked this other question:
Attaching JavaScript to the prototype of an ASCX client side instance

The previous question answered the how to do it, but now I am curious how this works on a pure Javascript/HTML level. And I’m no closer to figuring it out.

Let assume I have an HTML page with just a textbox:

<html>
   <body>
    <div>
     <input type="text" id="MyTextBox" />
    </div>
   </body>
</html>

In a browser, I can do document.getElementById('MyTextBox').

My question is however, using just javascript, how can I assign the object returned a javascript type so that from the object I can call functions defined in the namespace?

For instance, I want to do:

var x = document.getElementById('MyTextBox');
x.SetTheText('blah');

and in my custom namespace/type/class I would have defined SetTheText as

function SetTheText(text) {
    this.value = text;
}

How do I say MyTextBox is an object that can run functions defined in a JS namespace.
I hope this makes sense

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:45:04+00:00Added an answer on May 24, 2026 at 3:45 pm

    Basically, you can add any kind of property to (almost) any kind of JS object. And if you add a function to an object, that function’s this will be the object.

    In other words

    var x = document.getElementById('MyTextBox'); // a DOM object
    x.setTheText = function(text) {
         this.value = text;
    };
    x.setTheText('blah'); // bingo
    

    If you want to extend an entire class of objects, you can do that too, via the prototype

    HTMLTextAreaElement.prototype.setTheText = function(text) {
        this.value = text;
    };
    someRandomTextArea.setTheText("blah"); // bingo. Again.
    

    However, this is not really recommended, as you’re messing with objects that are outside your control (i.e. it’s fragile as other scripts and browser updates and whatnot might get in the way). Also, you might break some other piece of code by doing this.

    A better solution (in many ways) is the jQuery solution of wrapping an unmodified DOM element in another object, and calling methods on the wrapper object rather than the element directly (Personally, I rather like the “pretty” code that can come from just extending native JS objects, but alas, it’s not safe, so I’m trying to quit that.)

    p.s. Classes are Captilized in javascript; methods are camelCase. I’ve edited the code accordingly. It’s not something that’s enforced by anything, but style’s style.


    Edit: For comparison, you can look at the prototype.js library, which works its magic by extending the DOM. Again, in my opinion, it makes for some very pretty code compared to jQuery’s constant invocations of $(...).xyz(...), but it’s still a slightly dangerous route to take

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can one ignore Unexpected element situation in JAXB ans still get all other
In order to return a value from a VB.NET function one can assign a
Can I assign more than one CssClass to a control in asp.net?How to do
Can one specify XML attribute values as CDATA ? If yes - what would
How can one detect being in a chroot jail without root privileges? Assume a
How can one load custom (not an image, nor a sound file) resource file
How can one remove all text, but leave the structure intact? for example: <animals>
How can one interface with Google Sketchup , via the API, from a Windows
How can one allow code snippets to be entered into an editor (as stackoverflow
How can one specify the connection string in a config file of a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.