using an array's extended properties in jQuery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • svendok
    New Member
    • Jun 2010
    • 25

    using an array's extended properties in jQuery

    What does one have to do in the code below to allow jQuery to make use of the find() function that I added to the array object (see line #26)?

    Is there a namespace clash?

    The code is here http://jsbin.com/oluyu6/5/edit

    Thanks.

    Code:
      Array.prototype.find = function(searchStr) {  
         // http://www.hunlock.com/blogs/Mastering_Javascript_Arrays#quickIDX39
         var returnArray = false;  
         for (i=0; i<this.length; i++) {
                 if (typeof(searchStr) == 'function') {
                     if (searchStr.test(this[i])) {
                         if (!returnArray) {     returnArray = [];  }
                         returnArray.push(i);      
                     }    
                 } else {
                   if (this[i]===searchStr) {
                       if (!returnArray) { returnArray = []; }
                     returnArray.push(i);      
                 }    
             }  
         }  
         return returnArray;
     };
    
    function toggleArrowBySelectedVal(option_array, list_id, arrow)
    {
      var list=$("#"+list_id);
      
      var numselected=$("#"+list_id+" option:selected").length;
      if ( (numselected==1)   &&
         (option_array.find(Number(list.value))))  arrow.disabled=false;
      else                      arrow.disabled=true;
      
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    list.value is undefined, because list is a jQuery instance and not a <select>.

    besides that you have a logic error (line #6), if searchStr is a function, searchStr.test( ) will throw an error (undefined method call). functions only have the methods apply(), bind(), call() & toString().
    Last edited by Dormilich; Apr 14 '11, 08:55 PM.

    Comment

    Working...