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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:14:18+00:00 2026-05-16T15:14:18+00:00

I’m writing a jQuery plugin that stores some data in some cases. I’d like

  • 0

I’m writing a jQuery plugin that stores some data in some cases.

I’d like to write it in a very flexible way, where I’ll can change an input parameter to obtain some value that were stored by the plugin.

Explanation:

When I call $("#any").myPlugin(), my plugin initializes creating a div and some a inside.
Clicking on an a will store it .index() using the .data() method.
If I call $("#any").myPlugin("getSelection") then I would like to get the value stored with .data().

What I’d tried:

(function ($) {
    $.fn.myPlugin = function (action) {
        if (action == null) action = "initialize";

        return this.each(function ($this) {
            $this = $(this);

            if (action == "initialize") {
                $this.html('<div></div>');
                var div = $("div", $this);

                div.append('<a>A</a>').append('<a>B</a>').append('<a>C</a>');

                div.children("a").each(function (i) {
                    $(this).click(function (event) {
                        // Here I store the index.
                        $this.data($(this).index());
                        event.preventDefault();
                        return false;
                    });
                });

                return $this;
            } else if (action == "getSelection") {
                // With this action, I tried to get the stored value.
                return $this.data("selectedValue");
            }
        });
    };
})(jQuery);

Simple call to create the elements:

$("#someElement").myPlugin();

And here I’d tried to get the index, without sucess:

alert($("#someElement").myPlugin("getSelection"));

So, is possible to do what I’m trying?

  • 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-16T15:14:19+00:00Added an answer on May 16, 2026 at 3:14 pm

    You need to change up the order a bit, like this:

    (function ($) {
        $.fn.myPlugin = function (action) {
            action = action || "initialize";
    
            if (action == "getSelection") {
              return this.data('index');
            }
    
            return this.each(function ($this) {
                $this = $(this);
    
                if (action == "initialize") {
                    $this.html('<div></div>');
                    var div = $("div", $this);
    
                    div.append('<a>A</a>').append('<a>B</a>').append('<a>C</a>');
    
                    div.children("a").each(function (i) {
                        $(this).click(function (event) {
                            // Here I store the index.
                            $this.data('index', $(this).index());
                            event.preventDefault();
                            return false;
                        });
                    });
    
                    return $this;
                }
            });
        };
    })(jQuery);
    

    The you can get the clicked index out like this:

    alert($("#someElement").myPlugin("getSelection"));
    

    You can give it a try here, the fundamental problem is you’re trying to return a single value out of a .each() loop, which doesn’t work. This instead grabs the data off the first object that matches the selector (#someElement in the example). Also .data() stores other things, so you need to give your value a key, like I’m using 'index' in the version above.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I am reading a book about Javascript and jQuery and using one of the

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.