views:

68

answers:

2

sample html:

<tr>
    <td class="hidden tblLnk">8163</td> 
</tr>
<tr>
    <td class="hidden tblLnk">8163</td> 
</tr>
<tr>
    <td class="hidden tblLnk">8164</td> 
</tr>

this method should return a unique array of text from rows with a specific td class. { 8163, 8164 } in our sample.

works in ffs and chrome but not in ie8 or safari. can you spot the problem?

function getUniqueIds()
{
     var tblLnks = new Array();

     $('td.tblLnk').each(function()
     {
        tblLnks.push($(this).text().trim());
     });

     return tblLnks.unique();
}
+1  A: 

1st: There is no native unique() method on the Array object in JavaScript that works in all A-grade browsers as of today. So if this is your intention, please post that code aswell.

2nd: If you refer to the unique() method of jQuery you better read up on the description of that method. This method can´t be called on the Array object. It takes an Array object of DOM elements as a paramenter, e.g.:

$.unique(myArrayOfDomElements);
anddoutoi
i added a unique function.Array.prototype.unique = function() { var a = []; var l = this.length; for(var i=0; i<l; i++) { for(var j=i+1; j<l; j++) { // If this[i] is found later in the array if (this[i] === this[j]) j = ++i; } a.push(this[i]); } return a; };
CurlyFro
+2  A: 

I think this:

$(this).text().trim()

should be this:

$.trim($(this).text());

If your intention is to us jQuery's trim() function.

patrick dw
yup -- that was it. thanks. i love stackoverflow :)
CurlyFro
As an addendum, String.trim() is only available in JavaScript 1.8.1/ECMAScript 5, which is implemented by Firefox 3.5. This explains the differences:https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/Trim
Peter