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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:31:24+00:00 2026-05-27T07:31:24+00:00

Is my third day with Javascript and jQuery and I´m playing a little with

  • 0

Is my third day with Javascript and jQuery and I´m playing a little with Literal Object structure to make the code more legible and modificable. ok. The problem…

I have written a script to insert dinamically inputs to a form and a button to display the content of inputs. Example:

jQuery('#add').click(function(){
   jQuery('#opts').append('<li><input type="text" name="i" value="i" /></li>');
});

jQuery('#view-list').click(function(){
    $itemsList = jQuery('#opts :input');
    console.log('there are ' + $itemsList.length + ' items');
    $itemsList.each(function(){
        console.log(jQuery(this).val());
    });
});

It works correctly, the “add” button inserts items and click button displays items.

The problem is when I take an approach to Literal Object. For example in a big app, this script it will be a little part of it. In this case i make it like this:

var sidebarManager = {

init : function(){
    sampleFunction0();
    sampleFunction1();
    sampleFunction2();  
},

sampleFunction0 : function(){

    jQuery('#add').click(function(){
        jQuery('#opts').append('<li><input type="text" name="insert" value="insert" /></li>');
    });

    jQuery('#view-list').click(function(){
        $itemsList = jQuery('#opts :input');
        console.log('there are ' + $itemsList.length + ' items');
        $itemsList.each(function(){
            console.log(jQuery(this).val());
        });
    });

},

sampleFunction1 : function(){ //code },

sampleFunction2 : function(){ //code }

}

jQuery(document).ready(function(){
    sidebarManager.init();
});

But in this case the list button only display the items who are on the list when the function is called via init function. If added new items, these are not displayed.

I’m interested in the use of literal objects and encapsuled solutions but I´m littel confused 😛

Any advice, sugestion, WTF, or whatever… 🙂
Thanks.

Edited

what happens in the situations with 2 ambiguous this? For exampe:

sampleFunction0 : function(){
    jQuery('#add').click(function(){
        jQuery('#opts').append('<li><input type="text" name="insert" value="insert" /></li>');
    });

    jQuery('#view-list').click(function(){
        var $itemsList = jQuery('#opts input');
        console.log('there are ' + $itemsList.length + ' items');
        $itemsList.each(function(){
            console.log(jQuery(this).val());
            var test = this.sampleFunction1(); //conflict
        });
    });
},

In this case is necesary call fuction1 like this:

sidebarManager.sampleFunction1();
  • 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-27T07:31:25+00:00Added an answer on May 27, 2026 at 7:31 am

    First, you have to fix the javascript error in the init() function by adding this:

    init: function() {
        this.sampleFunction0();
        this.sampleFunction1();
        this.sampleFunction2();
    },
    

    sampleFunction0, sampleFunction1, and samplefunction2 only exist as properties on the sidebarManager object so you have to reference them as properties on that object. This should have been easy for you to find if you were looking in your browser’s error console or debug console (where javascript interpreter errors are displayed).

    Then, I see a couple other issues that should be cleaned up. First, you are using undeclared variables like $itemsList. That makes them global variables by default. I don’t think you want that and it could potentially cause issues in your code (conflict with other code also doing that). You should preface their first use inside your function with var to make them local variables. Implicit global variables are a frequent source of accidental bugs and are a bad thing. Avoid them.

    Second, I don’t think your selector is ideal. Instead of '#opts :input', I think you want this '#opts input' which will fetch any input tags that are contained in the #opts object. Both will work in some cases, but I think the second better represents what you want here and is probably a little faster. The resulting code with both fixes would look like this:

    sampleFunction0 : function(){
    
        jQuery('#add').click(function(){
            jQuery('#opts').append('<li><input type="text" name="insert" value="insert" /></li>');
        });
    
        jQuery('#view-list').click(function(){
            var $itemsList = jQuery('#opts input');
            console.log('there are ' + $itemsList.length + ' items');
            $itemsList.each(function(){
                console.log(jQuery(this).val());
            });
        });
    },
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

THIRD EDIT: I now believe that this problem is due to a SOAP version
I have a javascript array of dates, in the form {year:'2010',month:'6',day:'23'} I need to
Ok this is third day of working with Xcode so please be nice. I
From Day One I was told that each third-party iOS app was perfectly sand-boxed
Greetings. I'm having a weird problem with jQuery on my website. I'm trying to
I'm now on my third full day this week of trying to get OpenCV
I came across some code the other day and I wondered if that was
There are cases where shipping javascript code with your flash video is not an
Good day! In his Effective STL Scott Meyers wrote A third is to use
I've spent the better part of the day so far looking into this problem.

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.