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 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
  • 1 View
  • 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

Related Questions

First off: I know that AES is a block cipher and not a hashing
First off I'm not sure that's the proper title for this question but hopefully
First off, before I ask, i would like to point out that this question
I know first off that this is probably the worst looking regex ever but
First off, there's a bit of background to this issue available on my blog:
First off, let me start off that I am not a .net developer. The
I hope this question does not come off as broad as it may seem
First off, I know this may be a very stupid question, so don't shoot
First off, does anyone know of a good place to get help with CodeIgniter?
First off I know this has been covered on SO. But the most popular

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.