0

As shown below, I am passing a variable from the inputclass callback to the validate callback.

Could it be done so without using a global variable so it will be local to the instance when applying the jQuery plugin to the given element?

$('#name').editable({
    inputclass: function() {
        globalVariable=this;
    },
    validate: function (value) {
        console.log(globalVariable);
        return 'pause';
    }
});

Fiddle

5
  • you can try print out "this" in inputClass or validate function to see whether it refers to the configuration object itself. If it is (high chance), you can just put variable as a property of this, both of those functions will refer to the same configuration object Commented Apr 11, 2015 at 5:27
  • @PhuongNguyen For this particular plugin, this within inputclass refers to a created input element while within validate refers to the originally element which the plugin was applied to. Commented Apr 11, 2015 at 5:30
  • try this: $.fn.editable.globalVariable . private for editable plugin.e.g: $.fn.editable.globalVariable =this; Commented Apr 11, 2015 at 6:21
  • Is this your plugin? If so, I think you should consider the option of passing that reference as a parameter to the validate callback. Commented Apr 11, 2015 at 6:47
  • @acontell Not my plugin, but agree that it should be added. Commented Apr 11, 2015 at 15:58

3 Answers 3

1

Since this in inputclass() and validate() are referring to different elements, it's a bit troublesome to share data. My idea is also using data(), but to pass data from inputclass() to validate(), you will need to travel up the dom from input that inputclass() refer to, up to .ui-tooltip, look for the element that validate() is referring to through id

$('#name').editable({
    inputclass: function(e, f) {
        $("a[aria-describedby=" + $(this).closest(".ui-tooltip").prop("id") + "]").data("shared", "somedata");
    },
    validate: function (value) {
        console.log("validate", $(this).data("shared"));
        return 'pause';
    }
});

Updated jsfiddle to demonstrate:

jsfiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Phuong, Seems to work :) Haven't fully tested, but each instance has its own dataset, right? jsfiddle.net/cgym6m3v/5
0

I would recommend using data attributes here to pass data.

$('#name').editable({
    inputclass: function() {
        var value = 'some value';
        $(this).data('input-value', value);
    },
    validate: function (value) {
        var inputValue = $(this).data('input-value');
        console.log(inputValue);
        return 'pause';
    }
});

This method will work only if you know the inputClass function gets called before validate

P.S: If you just want to pass this, there is absolutely no need for that, since it will be bind automatically in both functions.

1 Comment

Thanks Sriharsha, this does not appear to be the same in the two callbacks, so I don't think this will work.
0

You need to define that variable outside the scope of functions: Try this:

  var globalVariable;
    $('#name').editable({
        inputclass: function() {
            globalVariable='your value';
        },
        validate: function (value) {
            console.log(globalVariable);
            return 'pause';
        }
    });

1 Comment

Thanks Maverick. Kind of what I was doing. Problem is any time the plugin is applied on the page, it will share the same variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.