How would I go about getting a variable from a jQuery plugin?

index page

$("#test-div").myplugin();
if(testvariable == 1){
alert("test variable equals 1");
}

The plugin page

(function($){
$.fn.myplugin = function(){  

var testvariable=1;

return testvariable
}
})(jQuery);

Recommended Answers

All 3 Replies

Use this:

$("#test-div").myplugin(function(callback){
if(callback == 1){
alert("test variable equals 1");
}
});
(function($){
$.fn.myplugin = function(callback){  

var testvariable=1;

callback(testvariable);
}
})(jQuery);

Thank you. it works great!

Your welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.