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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:05:43+00:00 2026-05-12T06:05:43+00:00

First off, I know this is a base JS issue, not jQuery. I am

  • 0

First off, I know this is a base JS issue, not jQuery. I am at that learning point where I cannot seem to get my head completely wrapped around some scope issues. So I need some explanation.

I’ve been reading every article/tutorial I can find, but just need some answers. I setup this base code to experiment with (as the actual code is far too big to post here).

In the following experiment, when the selector1-change button is clicked the ‘cmd’ variable is displayed correctly, but the reference to this.options is ‘undefined’, because it is a different instance of MySelector(), unless I am way off.

So how does one go about calling an instance of an object like this.

Example:

You can use jQueryUI’s dialog by creating it, and then later you can pass commands to it like $(‘#mydiv’).dialog(‘close’); and that will access the instance attached to that element. How is that accomplished?

JQUERY_MYSELECTOR.JS

(function($){
    $.fn.MySelector = function(incoming) {

        if (typeof(incoming) != 'object') {
            return runCommand(incoming);
        }

        var options = $.extend({},{
            id: false,
            backgroundColor: false
        },incoming);

        function runCommand(cmd) {
            alert('blah:'+this.options.backgroundColor+"\n"+'cmd:'+cmd);
        }
    }
})(jQuery);

SCRIPT

<script type="text/javascript" src="jquery_myselector.js"></script>
<script type="text/javascript">
    $j(document).ready(function() {
        $j('#selector1).MySelector({ backgroundColor: '#000000' });
        $j('#selector1-change').click(function() {
            $j('#selector1').MySelector('mooocow');
        });
    });
</script>

STYLE

<input type="button" value="selector1" id="selector1" />
<br/>
<input type="button" value="selector1-change" id="selector1-change" />

UPDATED
Forgot to use the plugin on selector1 (duh)

  • 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-12T06:05:44+00:00Added an answer on May 12, 2026 at 6:05 am

    You’re calling runCommand before you assign a value to options – options has the value undefined inside the body of runCommand. Don’t ask me how JS is able to see the body of runCommand before you declare it, but it can.

    Also, at the first level of MySelector, this is bound to the results of running $j('#selector1-change'), which is an array. You will need to iterate that array.

    Try something like this:

    (function ($) {
        function runCommand(e, cmd) {
            alert('blah:' + e.options.backgroundColor + "\n" + 'cmd:' + cmd);
        }
    
        $.fn.MySelector = function (incoming) {
            this.each(function () {
                if (typeof (incoming) != 'object') {
                    return runCommand(this, incoming);
                } else {
                    this.options = $.extend(this.options || {
                        id: false,
                        backgroundColor: false
                    }, incoming);
                }
            });
        }
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 219k
  • Answers 219k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's a trick to make progress quickly with these aspects… May 12, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer But you could have a public constructor, right? So if… May 12, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer Function str_ireplace doesn't change its arguments. It returns the result.… May 12, 2026 at 11:49 pm

Related Questions

I'm trying to use JQuery's $.getJSON to access Model data from the Controller, and
First, let me begin by telling you the details on the problem I'm trying
I've been perplexed over a .NET dilemma for the past couple months. I can't
First a little about myself. I am not an experienced software engineer, architect or

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.