1

I have a jquery plugin
suppose to show alert
html page is as

<html>
<head>
<script src='jquery.js'></script>
<script src='myPlugin.js'></script>
<script>
 $().ready(function(){
   $(".txt").my-plugin({},function(e){
        alert(e.a +" "+ e.b);
    });
 });
</script>
</head>
<body>
   <input type='text' class='txt' />
   <input type='text' class='txt' />
   <input type='text' class='txt' />
</body>
</html>

and my plugin is as below

;(function ($) {
$.fn.myPlugin = function (options,callback) {       
  var defaultVal = {
  vars:'values'
  };
  var obj = $.extend(defaultVal, options);//overwrite default values if there
  return this.each(function(){
    //my other code
  $(this).click(function(){

    //my code

      var a="some value";
      var b="more values";
      if (typeof callback == 'function') 
      {
          callback.call($(this));//how can I send var a,b here
      }
   });
   })
   };   
 })(jQuery);

Q1: How can I send values from plugin to be accessed in callback function?
I have to send attribute values

Q2: How can I place the callback function in Options?

1 Answer 1

2

It should be

$(".txt").alamStar({},function(e){
    alert(e.a +" "+ e.b);
});

because the function name is alamStar...

Some tweak here

$(function(){
   $(".txt").alamStar({
      callback: function(e){
        alert(e.a +" "+ e.b);
      }
   });
 });


;(function ($) {
$.fn.alamStar = function (options) {       
  var defaultVal = {
    vars: 'values',
    callback: null
   };

 var defaultVal = $.extend(defaultVal, options);//overwrite default values if there

 return this.each(function(){
  //my other code
  $(this).click(function(){

 //my code

  var a="some value";
  var b="more values";

  if (typeof defaultVal.callback == 'function') 
      defaultVal.callback({a:a, b:b});//how can I send var a,b here

  });
 })
};   
})(jQuery);
Sign up to request clarification or add additional context in comments.

1 Comment

ohh yes,, I hv copied and edited here..... but how can i send a and b from plugin?

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.