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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:37:47+00:00 2026-05-30T15:37:47+00:00

Here is the code: http://jsfiddle.net/GKBfL/ I am trying to get collection.prototype.add to return a

  • 0

Here is the code:

http://jsfiddle.net/GKBfL/

I am trying to get collection.prototype.add to return a reference such that the final alert will display testing, testing, 123, testing. Is there a way to accomplish what I’m trying to do here?

HTML:

<span id="spantest">testing, testing, 123, testing</span>​

JavaScript:

var collection = function () {
   this.items = {};   
}

collection.prototype.add = function(sElmtId) {
    this.items[sElmtId] = {};
   return this.items[sElmtId];
}

collection.prototype.bind = function() { 
    for (var sElmtId in this.items) {
        this.items[sElmtId] = document.getElementById(sElmtId);
    }
}
var col = new collection();
var obj = {};
obj = col.add('spantest');
col.bind();
alert(obj.innerHTML);​
  • 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-30T15:37:48+00:00Added an answer on May 30, 2026 at 3:37 pm

    You problem is this line:

    this.items[sElmtId] = document.getElementById(sElmtId);
    

    This overwrites the object currently assigned to this.items[sElmtId] with the DOM node. Instead, you should assign the node to a property of that object:

    this.items[sElmtId].node = document.getElementById(sElmtId);
    

    That way, obj.node will always refer to the current node:

    alert(obj.node.innerHTML);
    

    DEMO


    Side note: The problem with your fiddle is also that you execute the code when the DOM is not built yet (no wrap (head)), so it cannot find #spantest. You have to run the code once the DOM is ready, either no wrap (body), onDomRead or onLoad.

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

Sidebar

Related Questions

I am fiddling with this javascript code here http://jsfiddle.net/7Sd4W/4/ I am trying to get
JSfiddle (showing the non-working code) here: http://jsfiddle.net/ts2Rr/ I have three boxes that a user
I want to get the content of multiple Spans. Here is my code: http://jsfiddle.net/4XumV/
Here is sample code: http://jsfiddle.net/BSjGX/1/ <input id=testing /> $('#testing') .keydown(function(e){ if(e.keyCode==13) {return false;} })
I am trying to position few elements correctly, here is my code: http://jsfiddle.net/Tjz6D/ Now
my code here http://jsfiddle.net/GKZRU/14/ all js working properly, and add/remove css classes(checking this in
Here's my code http://jsfiddle.net/6748w/ I would like that the imgs/fleche_bleue.png in my CSS to
I have some code here: http://jsfiddle.net/zkRq2/2/ that I am having issues with. If you
I'm trying to create a header menu. Please see my code here: http://jsfiddle.net/kc2cc/ .
I am trying to get the code found here: http://snipplr.com/view/26643/mbprogresshud-with-an-asynchronous-nsurlconnection-call/ to work in my

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.