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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:58:46+00:00 2026-05-24T12:58:46+00:00

I have a jQuery plugin: (function( $ ){ $.fn.test = function(selector){ $this = this;

  • 0

I have a jQuery plugin:

(function( $ ){
    $.fn.test = function(selector){
        $this = this;

                $(selector).bind("click", function(){
                    methods.showvalue($this);
                });
    };
    var methods = { 
        showvalue   :   function($this){
            var output = "";
            $this.each(function(i,e){
                output += $(e).val();
            })
            alert(output);
        }
    }
})(jQuery);

With this markup:

<form>
    <p>
        First section<br />
        <input type="text" class="test" name="firstValue" value="one" />
        <input type="text" class="test" name="secondValue" value="two" />
        <input type="text" class="test" name="thirdValue" value="three" />
        <button type="button" id="button1">push</button>
    </p>
    <p>
        Second section<br />
        <input type="text" class="test2" name="firstValue2" value="1" />
        <input type="text" class="test2" name="secondValue2" value="2" />
        <input type="text" class="test2" name="thirdValue2" value="3" />
        <button type="button" id="button2">push</button>
    </p>
</form>

and this initializer:

    $(document).ready(function(){

        // first section
        $(".test").test("#button1");

        // second section
        $(".test2").test("#button2");
    });

Regardless of which button I push I always get a string with “123” in it. My goal is to get “123” when the second button gets pressed, and “onetwothree” one the first button is pressed.

What am I missing? Why isn’t it behaving that way?

  • 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-24T12:58:47+00:00Added an answer on May 24, 2026 at 12:58 pm

    You need to declare

    var $this = this;
    

    Otherwise, $this gets attached to the window and becomes global.

    Also, you might want this instead, so you don’t attach several callbacks:

    $.fn.test = function(selector){
        var $this = this;
        $(selector).bind("click", function(){
           methods.showvalue($this);
        });
        return this;
    };
    

    EDIT: Demo http://jsfiddle.net/VSDLJ/

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

Sidebar

Related Questions

in a jQuery plugin i have created helper functions, like this (function($) { var
I have written a jQuery plugin like this: jQuery(function($) { $.fn.myPlugin = function() {
I have seen the following script from jQuery Validation Plugin. http://docs.jquery.com/Plugins/Validation/Methods/email email: function(value, element)
I have a jquery plugin X and inside the plugin I have a function
Say I have a jQuery plugin like $.abc = function() {}. Now I want
This is about the (mootools-like) kwicks Jquery plugin: http://www.jeremymartin.name/projects.php?project=kwicks I have the same problem
The jquery datepicker plugin works, but I'd like to have onSelect triggering a function
I am using jQuery Validation Plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) I have wrote following rule jQuery.validator.addMethod(valueNotEquals, function(value,
I have a following code: $(document).ready(function() { $('a[rel]').each(function() { $(this).qtip( { content: { text:
How do you rotate an image using jQuery-rotate plugin? I have tried the following

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.