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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:31:19+00:00 2026-05-28T01:31:19+00:00

here is my code for a custom jquery plugin : (function($){ $.fn.extend({ getmyValue :

  • 0

here is my code for a custom jquery plugin :

(function($){
    $.fn.extend({
        getmyValue : function(){
        return this.each(function() {
                return this.myVal;
        });
        },
        initPlugin : function(){
        return this.each(function() {
                this.myVal='Some results';
        });
        }
    });
})(jQuery);

when i run this code :

$(document).ready(function() {

$("#div").initPlugin();
alert($("#div").getmyValue());
});

the returned value is not a plain string as supposed but an object ( $(“#div”) is returned )

what i can’t figure out is why the return chaining is not working ?

  • 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-28T01:31:20+00:00Added an answer on May 28, 2026 at 1:31 am

    Because the return value of each is the object on which you called each. The return value of the function each calls is used to determine whether to stop looping (that is, the iteration function can return false to stop looping — docs link).

    It’s unclear from your code what you really want to do in getmyValue; return a value you’ve stored on the jQuery instance itself? Return the myVal stored on the first contained element? Return an array of the myVal values from all the contained elements?

    If you meant a myVal stored on the jQuery instance by your plugin:

    getmyValue : function(){
        // Here, `this` is the jQuery instance on which `getmyvalue` was called
        return this.myVal;
    },
    

    If you meant the myVal on the first element (note that it’s a raw DOM element in the typical case):

    getmyValue : function(){
        // Here, `this` is the jQuery instance on which `getmyvalue` was called.
        // `this[0]` is the first matched element (a raw DOM element, typically).
        // Note we check before accessing it to see if it's there, since a jQuery
        // instance may have matched zero elements.
        return this[0] ? this[0].myVal : undefined;
    },
    

    If you meant an array of the myVal values from all the matched elements (again, these will be raw DOM elements in the typical case):

    getmyValue : function(){
        // Here, `this` is the jQuery instance on which `getmyvalue` was called.
        return this.map(function() {
                // Here, though, `this` one of the elements wrapped by the jQuery,
                // instance typically a raw DOM element. (Each of them in a loop.)
                return this.myVal;
        }).get();
    },
    

    …which uses map to get a jQuery-wrapped array of the values, and then get to get the raw array from it.

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

Sidebar

Related Questions

This is jQuery code to create a custom plugin. (function( $ ){ var methods
So I have this jQuery Validation plugin, and it has a custom success message.
Why is this jquery image watermarking plugin not working? I am using the code
I am trying to refine my custom jquery plugin. I have asked this before
I implemented a custom Profile object in code as described by Joel here: How
Ok the error is showing up somewhere in this here code if($error==false) { $query
Edit: The code here still has some bugs in it, and it could do
See here: http://code.google.com/p/ie7-js/ Does anyone have any experience or remarks about this javascript? Is
I have this code here: var infiltrationResult; while(thisOption) { var trNode = document.createElement('tr'); var
qTip...a jQuery custom tooltip plugin... isn't loading dynamic content for IE8 (haven't tested in

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.