Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple selector using jQuery

i am using a jquery plugin and my code look somthing like this.

<script type="text/javascript">
$(document).ready(function() {
    $('#fileUpload').uploadify({ 
      'uploader': 'img/uploadify.swf',
      'script': 'uploadify.php',
      'folder': 'upload',
      'auto' : 'true',
      'cancelImg': 'img/cancel.png',
      'fileDesc': 'jpg/jpeg',
      'displayData': 'percentage',
      'fileExt': "*.jpg;*.jpeg",
      'sizeLimit' : '8388608',
      'fileDataName' : 'file',
      onComplete: function(event, queueID, fileObj, reposnse, data) 
      {
     $('#filesUploaded').append('<a href='+fileObj.filePath+'>'+fileObj.name+'</a><br>');
     $("#firstUpload").remove();

        }
      }); }); 

$(document).ready(function() {
    $('#fileUpload2').uploadify({ 
      'uploader': 'img/uploadify.swf',
      'script': 'uploadify.php',
      'folder': 'upload',
      'auto' : 'true',
      'cancelImg': 'img/cancel.png',
      'fileDesc': 'jpg/jpeg',
      'displayData': 'percentage',
      'fileExt': "*.jpg;*.jpeg",
      'sizeLimit' : '8388608',
      'fileDataName' : 'file',
      onComplete: function(event, queueID, fileObj, reposnse, data) 
      {
     $('#filesUploaded').append('<a href='+fileObj.filePath+'>'+fileObj.name+'</a><br>');
     $("#firstUpload").remove();

     }
      }); }); 
      </script>

did you notice that i am using the exact same function and i am just changing the div name, ain't that ridiculous ? now i want my jquery function to accept two div parameters. can i do that?

thank you..

like image 359
Ibrahim Azhar Armar Avatar asked Sep 05 '10 04:09

Ibrahim Azhar Armar


People also ask

Can we use multiple selectors in jQuery?

jQuery - Multiple Elements SelectorYou can specify any number of selectors to combine into a single result.

Can we combine multiple selectors in a single jQuery function call?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

Can you chain jQuery selectors?

However, there is a technique called chaining, that allows us to run multiple jQuery commands, one after the other, on the same element(s). Tip: This way, browsers do not have to find the same element(s) more than once. To chain an action, you simply append the action to the previous action.

How many types of jQuery selectors are there?

The most important functionality of jQuery is provided by it's Selectors. This tutorial will explain jQuery Selectors with simple examples covering all the three standard selectors.


2 Answers

Combine the two selectors, just like in CSS:

$(document).ready(function() {
    $('#fileUpload, #fileUpload2').uploadify({ 
        ...
    });
});
like image 92
BoltClock Avatar answered Sep 25 '22 13:09

BoltClock


<script type="text/javascript">
$(document).ready(function() {
    $('#fileUpload,#fileUpload2').uploadify({ 
      'uploader': 'img/uploadify.swf',
      'script': 'uploadify.php',
      'folder': 'upload',
      'auto' : 'true',
      'cancelImg': 'img/cancel.png',
          'fileDesc': 'jpg/jpeg',
      'displayData': 'percentage',
      'fileExt': "*.jpg;*.jpeg",
      'sizeLimit' : '8388608',
      'fileDataName' : 'file',
      onComplete: function(event, queueID, fileObj, reposnse, data) {
        $('#filesUploaded').append('<a href='+fileObj.filePath+'>'+fileObj.name+'</a><br>');
        $("#firstUpload").remove();
      }
    }); }); 
</script>
like image 26
Yanick Rochon Avatar answered Sep 25 '22 13:09

Yanick Rochon