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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:13:04+00:00 2026-05-19T01:13:04+00:00

I have a js function that is named getID which is basically return document.getElementById(id)

  • 0

I have a js function that is named getID which is basically return document.getElementById(id)

I want to make another function, getTag that would return getElementsByTagName.

The part that I can’t seem to manage is that I want to be able to call them like this:

getID('myid').getTag('input') => so this would return all the input elements inside the element with the id myid

Thanks!

ps: getTag would also have to work if it’s called by it’s own, but then it would just return document.getElementsByTagName

UPDATE:

Thanks to all that have replied! Using your suggestions I came up with this, which works well for me:

function getEl(){
    return new getElement();
}

function getElement() {
    var scope = document;

    this.by = function(data){
        if (data.id)    scope = scope.getElementById(data.id);
        if (data.tag)   scope = scope.getElementsByTagName(data.tag);

        return scope;
    }
}

and I use it like this:

var inputs = getEl().by({id:"msg", tag:"input"});
  • 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-19T01:13:04+00:00Added an answer on May 19, 2026 at 1:13 am

    Basically, you’re going to create a single object that contains each of your methods and also stores all data returned by the native functions. It would look something like this (not tested, but you get the idea):

    var MyLib = {
        getID: function(id) {
            var element = document.getElementById(id);
            this.length = 1;
            this[0] = element;
            return this;
        },
        getTag: function(tag) {
            var elements;
            if (this.length) {
                for (var i = 0; i < this.length; i++) {
                    var byTag = this[i].getElementsByTagName(tag);
                    for (var j = 0; j < byTag.length; j++) {
                        elements.push(byTag[j]);
                    }
                }
            }
            for (var i = 0; i < elements.length; i++) {
                this[i] = elements[i];
            }
            this.length = elements.length;
            return this;
        }
    };
    

    You can then use it like this:

    var elements = MyLib.getID('myid').getTag('input');
    for (var i = 0; i < elements.length; i++)
        console.log(elements[i]); // Do something
    

    The only real problem with this approach (besides it being very tricky and hard to debug) is that you have to treat the result of every method like an array, even if there is only a single result. For example, to get an element by ID, you’d have to do MyLib.getID('myid')[0].

    However, note that this has already been done before. I recommend you take a look at jQuery, if only to see how they accomplished this. Your code could be simplified to this:

    $("#myid input")
    

    jQuery is more lightweight than you think, and including it on your page will not slow it down. You have nothing to lose by using it.

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

Sidebar

Related Questions

No related questions found

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.