jquery plugin - how to make $(this) working in the context of a custom setting -


below simplified version of idea of want do.

i trying set custom option below how use $(this) in wring context. how write main object gets used , not in function, right fnode error.

beforedothis: function () {                           $(this).css({color:'blue'});                         alert($(this).text());                      }  

http://jsfiddle.net/dpeae/

html

<div>         <div id="one">test1</div>      <br />     <br />     <br />      <div id="two">test2</div>      <br />     <br />     <br />      <div id="three">test3</div>   </div> 

javascript

  /* moved own custom js plugin file ------------------------------------------- */                  //custom plugin                 $.fn.addmyspan = function (customoptions) {                      var defaults = {                         textcolor: "#033",                         beforedothis: function () {}                     };                      var settings = $.extend({}, defaults, customoptions);                     return this.each(function () {                         settings.beforedothis();                    });                  };          /* -----------------------------------------------------------------------------------------------------*/            $(document).ready(function () {              $('#one,#two,#three').addmyspan({                 textcolor: "red",                 beforedothis: function () {                       $(this).css({color:'blue'});                     alert($(this).text());                  }              });             }); 

store reference this set function's context call:

            var self = this;              var defaults = {                 textcolor: "#033",                 beforedothis: function () {}             };              var settings = $.extend({}, defaults, customoptions);              return this.each(function () {                 settings.beforedothis.call(self);             }); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -