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

  • SEARCH
  • Home
  • 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 8956723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:50:45+00:00 2026-06-15T14:50:45+00:00

SIMPLIFIED EXAMPLE CODE: var $ = function(selector, node) { // Selector engine var selector

  • 0

SIMPLIFIED EXAMPLE CODE:

var $ = function(selector, node) { // Selector engine
    var selector = selector.trim(), node = node || document.body;
    if (selector != null) {
        return Array.prototype.slice.call(node.querySelectorAll(selector), 0); }
    }
}

I want to use it like this…:

$("div").innerHTML='It works!';

…not like this…:

$("div")[0].innerHTML='It works only on the specified index!';

…or this:

for(i=0;i<$('div');i++) {
        $("div")[i].innerHTML='It works great but it's ugly!';
}

This is as close as I got. I would like chaining to work and for it to be compatible with native methods:

if(!Array.prototype.innerHTML) { 
    Array.prototype.innerHTML = function(html) {
        for (var i = 0; i < this.length; i++) {
            this[i].innerHTML = html;
        }
    }
}

$("div").innerHTML('It works, but it ruins method chaining!');

I decided to build this engine to better learn JavaScript; It’s working but I am hoping I can learn some more from the kind members of Stack Overflow. Any help would be much appreciated!

  • 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-06-15T14:50:45+00:00Added an answer on June 15, 2026 at 2:50 pm

    I want to use it like this…:

    $("div").innerHTML='It works!';
    

    …not like this…:

    $("div")[0].innerHTML='It works only on the specified index!';
    

    It sounds like you want to have assigning to innerHTML on your set of results assign to the innerHTML of all of the results.

    To do that, you’ll have to use a function, either directly or indirectly.

    Directly:

    var $ = function(selector, node) { // Selector engine
        var selector = selector.trim(),
            node = node || document.body,
            rv;
        if (selector != null) {
            rv = Array.prototype.slice.call(node.querySelectorAll(selector), 0); }
            rv.setInnerHTML = setInnerHTML;
        }
        return rv;
    }
    function setInnerHTML(html) {
        var index;
    
        for (index = 0; index < this.length; ++index) {
            this[index].innerHTML = html;
        }
    }
    
    // Usage
    $("div").setInnerHTML("The new HTML");
    

    There, we define a function, and we assign it to the array you’re returning as a property. You can then call that function on the array. (You might want to use Object.defineProperty if it’s available to set the setInnerHTML property, so you can make it non-enumerable.)

    Indirectly (requires an ES5-enabled JavaScript engine):

    var $ = function(selector, node) { // Selector engine
        var selector = selector.trim(),
            node = node || document.body,
            rv;
        if (selector != null) {
            rv = Array.prototype.slice.call(node.querySelectorAll(selector), 0); }
            Object.defineProperty(rv, "innerHTML", {
                set: setInnerHTML
            });
        }
        return rv;
    }
    function setInnerHTML(html) {
        var index;
    
        for (index = 0; index < this.length; ++index) {
            this[index].innerHTML = html;
        }
    }
    
    // Usage
    $("div").innerHTML = "The new HTML";
    

    There, we use Object.defineProperty to define a setter for the property.

    In the comments below you say

    I have a few prototypes that work when individually attached to the $ function. Example: $('div').makeClass('this'); They do not work when they are chained together. Example: $('div').makeClass('this').takeClass('that');

    To make chaining work, you do return this; from each of the functions (so the end of makeClass would do return this;). That’s because when you’re chaining, such as obj.foo().bar(), you’re calling bar on the return value of foo. So to make chaining work, you make sure foo returns this (the object on which foo was called).

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

Sidebar

Related Questions

When I post the following to node (simplified example): var xhr = new XMLHttpRequest();
I have a following (simplified) code: var myModule = { submitDummyForm: function(){ console.log(this); //
I've got following (simplified for example purpose) code and it works: void log(const string
Note that all the code is a simplified example in order to only communicate
I simplified my code for next example. So, please don't be wondered why I'm
I've simplified my JavaScript code to the example below; this code is giving me
I have the following code (asp.net-mvc, jquery) (i have simplified the example to show
Here is a simplified example of what i'm working with, working around preexisting code:
This is a simplified example, but I am working on a code translator that
Here's a simplified code example for what I'm doing. foo.js (in lib directory): exports.foo

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.