16

I have the following code:

var selected = $('#hiddenField').val().split(",");
...
if (selected.indexOf(id) > 0) {
   ... set value ...
}

I'm dynamically creating a CheckBoxList, and trying to remember the state of the checkboxes by putting the selected IDs into the hidden field.

I get an error stating that "Object doesn't support this property or method". My assumption is that selected is an array, which should support indexOf. Is that incorrect?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
chris
  • 36,094
  • 53
  • 157
  • 237
  • 1
    Just as a hint, these are normal **Javascript** methods, they are not jQuery specific. – Felix Kling Apr 09 '10 at 15:15
  • It'd probably help if you were to post more code. Also, you should verify that what you *think* you're getting out of that hidden field is really what's in it. – Pointy Apr 09 '10 at 15:17
  • @Felix: I wasn't sure if there was a difference between the built-in split and the jquery split. – chris Apr 09 '10 at 17:06
  • 1
    For a thorough explanation of the issue as well as a work around not only for indexOf but the other missing array functions in IE check out the StackOverflow question http://stackoverflow.com/questions/2790001/fixing-javascript-array-functions-in-internet-explorer-indexof-foreach-etc – Luis Perez Jan 11 '12 at 02:44

3 Answers3

22

There's an jQuery method to overcome the lack of indexOf(), you can use .inArray() instead:

var selected = $('#hiddenField').val().split(",");
if ($.inArray(id, selected) > -1) {
   ... set value ...
}

jQuery.inArray() exists for just this reason...if you're including jQuery already, no need to write the function again. Note: This actually returns a number, like indexOf() would.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • This seems like it should work, for some reason it's not: >? line.id 101 >? selected {...} [0]: "101" length: 1 >? $.inArray(line.id, selected) -1 > – chris Apr 09 '10 at 17:16
21

Based on your error message, I'm assuming this is coming from Internet Explorer.

Surprise! Internet Explorer (including version 8) does not support indexOf for arrays.

Here is Firefox's implementation you can use:

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
Matt
  • 43,482
  • 6
  • 101
  • 102
  • 1
    @Matt: Thank you. I thought they'd implemented it in IE8, so went and did a quick experiement, and was pleasantly surprised to see it in IE7 as well. As you can guess, the experiment was flawed (I forgot to remove the reference to Prototype, which conveniently adds it if it's missing). Thanks again. – T.J. Crowder Apr 09 '10 at 15:30
0
[].indexOf || (Array.prototype.indexOf = function(v,n){
  n = (n==null)?0:n; var m = this.length;
  for(var i = n; i < m; i++)
    if(this[i] == v)
       return i;
  return -1;
});
Ivo Sabev
  • 5,230
  • 1
  • 26
  • 38