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 1055673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:36:00+00:00 2026-05-16T17:36:00+00:00

Updated title to better reflect what I’m trying to do. In short, there are

  • 0

Updated title to better reflect what I’m trying to do.

In short, there are different constructors for different dom elements, and they don’t seem to all share a common prototype. I’m looking for a way to add a function property to every DOM element by modifying these prototypes, but I’m not sure how to find them.

For example, I could do something like this:

function enhanceDom (tagNames, methods) {
  var i=-1, tagName;
  while (tagName=tagNames[++i]) {
    var tag=document.createElement(tagName);
    if (!(tag && tag.constructor)) continue;
    for (var methodName in methods) {
      tag.constructor.prototype[methodName]=methods[methodName];
    }
  }
}

var thingsToEnhance = ['a','abbr','acronym','address'/* on and on... */];

enhance(thingsToEnhance, {
  doStuff : function(){
    /* ... */
  },
  doOtherStuff : function(){
    /* ... */
  } 
  /* ... */
});

Of course, I’d like to do this without listing every single html element. Can anyone think of a better way?

(Original question follows)

Goal – make getElementsByClassName work on any DOM node in any browser.

It’s been done before (sort of), but here’s my shot at it.

The question I have is, is there a good way to make this work with dynamically created elements? It seems that HTML DOM elements don’t share a common predictable prototype where getElementsByClassName could be added… Or am I missing something?

Here’s what I’ve got so far (edit – updated per discussion).

(function(){

  var fn = 'getElementsByClassName'; 
  // var fn = 'gEBCN'; // test

  if (typeof document[fn] != 'undefined') return;

  // This is the part I want to get rid of...
  // Can I add getByClass to a single prototype
  // somewhere below Object and be done with it?

  document[fn]=getByClass;
  withDescendants(document, function (node) {
    node[fn]=getByClass;
  });

  function withDescendants (node, callback, userdata) {
    var nodes = node.getElementsByTagName('*'), i=-1;
    while (node=nodes[++i]) {
      callback(node, userdata);
    }
    return userdata;
  }

  function getByClass (className) {
    return withDescendants(this, getMatches, {
      query:new RegExp('(^|\\s+)' + className + '($|\\s+)'), 
      found:[]
    }).found;
  }

  function getMatches (node, data) {
    if (node.className && node.className.match(data.query)) {
      data.found.push(node);
    }
  }

}());

It works well on content loaded before the script loads, but new dynamically-created elements won’t get a getElementsByClassName method. Any suggestions (besides setInterval, please)?

  • 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-16T17:36:00+00:00Added an answer on May 16, 2026 at 5:36 pm

    I think what you want can be achieved by prototyping the Element interface, like

    Element.prototype.getElementsByClassName = function() {
        /* do some magic stuff */
    };
    

    but don’t do this. It doesn’t work reliably in all major browsers.

    What you’re doing in the example in your question is not advisable, too. You’re actually extending host objects. Again, please don’t do this.

    You’ll fall in exactly those pitfalls Prototype ran into.

    I don’t want to merely copy Kangax’ article, so please read What’s wrong with extending the DOM.

    Why do you want this in the first place? What’s your goal?

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

Sidebar

Related Questions

Update: Modified title to better reflect my question Hi everybody :) My question today
EDIT: Updated code to better reflect my problem this code returns 9 strings to
I am trying to update the title of a button programmatically with an integer
Sorry for the bad title (couldn't think of a better way to describe it)
I apologise for the unclear title, I cannot think of any better one. I'm
Background: I would like a better replacement for the default title tag tooltip, one
I'm trying to pass a value to Page.Title in a nested WebForms Master Page,
Sorry for the confusing title, but I couldn't find a better way to explain
[This is an updated version of a question posted earlier, the previous title was
sorry the issue title is kinda vague. I think i can better explain it

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.