Write own JQUERY Plugin for multiple instances

Write own JQUERY Plugin for multiple instances

Hi there,

I'm a newbie when it comes to writing plugins, but I like to learn .

I got the following wish. I want to write a plugin that turns a "div" with an id to a uploadform for an image file. By setting options I want to pass values for height/size/extension etc.

I want to be able to use the plugin more then once on a page. for uploading a photo and for uploading an Avatar.

I made this, but this only works for the last called div. This means that both divs are generated good, but the submit event only triggers the second upoadform and the plugin wont work. It works fine when I only have 1 instance on the page. When I click the submit button I only want to submit the form from wich the button is.

Anyone got an idea?

Tnx in advance

ps, I know it's not beautifull code, but as said, I'm a newbdie

I create the form with this:

JQUERY
  
  1. $('#uploadPhoto').ws_uploadForm();
HTML:
  1. <div id="uploadPhoto"></div>


    
  1. (function($){
  2.     $.fn.extend({
  3.         ws_uploadForm: function(options) {
  4.             var defaults = {
  5.                 imagePath:      "images/",
  6.                 formAction:     "upload.php",
  7.                 uploadPath:     "uploadedImages/",
  8.                 defaultImage:   "unknown.png",
  9.                 imageMess:      "250,250",
  10.                 previewHeight:  "100",
  11.                 imageSize:      "1",
  12.                 extensions:     "jpg|jpeg|png|gif"
  13.             }
  14.             var options =  $.extend(defaults, options);
  15.             return this.each(function() {
  16.                 //-------------------------------------------------
  17.                 // The vars that are used in this plugin
  18.                 //-------------------------------------------------
  19.                 //---- The options that are set
  20.                 var o = options;
  21.                 //---- The upload errors
  22.                 var uploadError =       new Array;
  23.                 uploadError[1]  =       'Bestand is niet geupload';                                                     // file is not uploaded
  24.                 uploadError[2]  =       'Dit is geen afbeeldingsbestand';                                       // No image file
  25.                 uploadError[4]  =       'Error tijdens het verplaatsen van het bestand';        // Error during move upload file
  26.                 uploadError[5]  =       'Bestand bestaat reeds';                                                        // File allready exsits
  27.                 uploadError[6]  =       'Error tijdens het resizen van een jpg bestand';        // Error resizing jpg
  28.                 uploadError[7]  =       'Error tijdens het resizen van een gif bestand';        // Error resizing gif
  29.                 uploadError[8]  =       'Error tijdens het resizen van een png bestand';        // Error resizing png
  30.                 uploadError[9]  =       'Error tijdens het creeeren van een jpg bestand';       // Imagecreate error for jpg
  31.                 //---- error[10]=   Resized, not realy an error but a succes :)
  32.                 uploadError[11] =       'Error tijdens het creeeren van een gif bestand';       // Imagecreate error for gif
  33.                 uploadError[12] =       'Error tijdens het creeeren van een png bestand';       // Imagecreate error for png
  34.                 uploadError[13] =       'Bestandstype niet toegestaan';                                         // Filetype not allowed
  35.                 uploadError[14] =       'Bestand is te groot';                                                          // Filetype not allowed                        
  36.                 //---- Defining the object
  37.                 obj             =       $(this);                                
  38.                 //---- Defining the ID that has been set
  39.                 uploadFormID    =       obj.attr('id')+'_';
  40.                 //---- When this form is used with ph
  41.                 //----- Then the photo needs to come out of the DB and is in the "rel"-attribute
  42.                 if(obj.attr('rel')==undefined){
  43.                     var showImage   =   o.imagePath+o.defaultImage;
  44.                 }else{
  45.                     var showImage   =   obj.attr('rel');
  46.                 }
  47.                 //---- The form with buttons
  48.                 var form    =   "\
  49.                 <form action='"+o.formAction+"' method='post' enctype='multipart/form-data' target='upload_target' id='"+uploadFormID+"submitfile'>\
  50.                     <img id='"+uploadFormID+"uploadImage' class='uploadImage' src='"+showImage+"' height='"+o.previewHeight+"px'/>\
  51.                     <img id='"+uploadFormID+"process' src='"+o.imagePath+"ajax-loader3.gif' />\
  52.                     <br /><br />\
  53.                     <input name='file' type='file' id='"+uploadFormID+"fileLocation' />\
  54.                     <input type='hidden' name='uploadPath' value='"+o.uploadPath+"' />\
  55.                     <input type='hidden' name='imageMess' value='"+o.imageMess+"' />\
  56.                     <input type='hidden' name='imageSize' value='"+o.imageSize+"' />\
  57.                     <input type='hidden' id='defaultImage' value='"+o.imagePath+o.defaultImage+"' />\
  58.                     <input type='submit' name='submitBtn' value='Upload' />\
  59.                     <br /><br />\
  60.                     <+o.extensions.replace(/\|/g,", ")+"</p>\
  61.                     <+parseFloat(o.imageSize)+" MB</p>\
  62.                     <br />\
  63.                     <+uploadFormID+"result' class='error'></p>\
  64.                 </form>\
  65.                 <iframe id='"+uploadFormID+"upload_target' name='upload_target' style='width:0;height:0;border:0px solid #fff;'>\
  66.                 </iframe>";
  67.                 //---- What to do when the upload file gives response
  68.                 stopUpload  =   function (response, filename){
  69.                     //---- If fileupload went good, insert image
  70.                     if (response == 3 || response == 10){
  71.                         $('#'+uploadFormID+'uploadImage').attr("src",filename);
  72.                         $('#'+uploadFormID+'process').hide();
  73.                         $('#'+uploadFormID+'fileLocation').val('');
  74.                         return false;
  75.                     } else {
  76.                         $('#'+uploadFormID+'process').hide();
  77.                         $('#'+uploadFormID+'result').html(uploadError[response]).show();
  78.                     }
  79.                     return true;
  80.                 }              
  81.                 //---- Insert the form
  82.                 obj.html(form);
  83.                 //---- Hide the processing-image
  84.                 $('#'+uploadFormID+'process').hide();              
  85.                 //---- Submit the form              
  86.                 $("#"+uploadFormID+"submitfile", obj).submit(function(){
  87.                         $('#'+uploadFormID+'process',obj).show();
  88.                         $('#'+uploadFormID+'result',obj).html('').hide();
  89.                 });
  90.                 //---- Do live check on extension, this is also checked extra in php
  91.                 $("#"+uploadFormID+"fileLocation", form).change(function(){
  92.                     var fileLocation    =       $("#"+uploadFormID+"fileLocation").val().toLowerCase();
  93.                     var ext             =       o.extensions;
  94.                     var filter          =       new RegExp("."+ext);
  95.                     if(!fileLocation.match(filter)){
  96.                         $('#'+uploadFormID+'result').html(uploadError[13]).show();
  97.                         $('#'+uploadFormID+'fileLocation').val('');
  98.                     }
  99.                 });
  100.             });
  101.         }
  102.     });    
  103. })(jQuery);