How to reference original bound element in plugin callback?

How to reference original bound element in plugin callback?

I have plugin that simply opens a modal box and allows users to select a value form a picklist. The plugin attaches an event listen to open the select/modal box. This selected value will then be returned to a callback function. In this call back I would also like to reference the actual element the plugin was bound to but I am not sure how to do this. In the code see this section: "//************How do I "


This is one of my first plugins so if there is a better way to do this please let me know. 


Thanks,

Jason


Here is the code on jsBin: http://jsbin.com/welcome/70094/edit


Also here is the code:

  1. <html>
  2. <body>
  3. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

  4. <!-- plugin -->
  5. <script>
  6. (function( $ ){
  7. $.fn.pillSelector = function( options ) {  
  8. // Create some defaults
  9. var settings = $.extend( {
  10. 'onSelect' : function(){}
  11. }, options);
  12. //Create event lister for select button when user selects value form a list
  13. $('#selectBtn').click(function(){
  14. //Return the selected value to the call back function
  15. settings.onSelect( $('#selectList').find('option:selected').val() );
  16. //Hide the select box / fake modal box
  17. $('#selectBox').hide();
  18. });
  19. //For each element this plugin is attached to create event that will open selector box
  20. return this.each(function() {        
  21. //Add event listen that will open select box
  22. $(this).bind('click',function(){
  23. $('#selectBox').show();
  24. });
  25. });
  26. };
  27. })( jQuery );
  28. </script>
  29. <!-- user script -->
  30. <script>
  31. $(document).ready(function(){
  32. $('.openSelector').pillSelector({
  33. onSelect : function(color){
  34. console.log(color + ' selected');
  35. //************How do I reference the original .openSelector element this plugin was bound to?
  36. console.log($(this));
  37. console.log(this);
  38. //Do I need to some how store it in the plugin and return it in the function?
  39. }
  40. });
  41. });
  42. </script>
  43. <a href="#" id="select1" class="openSelector">open selector</a>
  44. <a href="#" id="select2" class="openSelector">open selector</a>
  45. <!-- pretend this is some kind of modal box -->
  46. <div id="selectBox" style="display: none;">
  47. <select id="selectList">
  48. <option value="red">red pill</option>
  49. <option value="blue">blue pill</option>
  50. </select>
  51. <input type="button" id="selectBtn" value="this would select something"/>
  52. </div>
  53. </body>
  54. </html>