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?

4
  • 1
    Just as a hint, these are normal Javascript methods, they are not jQuery specific. Apr 9, 2010 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 9, 2010 at 15:17
  • @Felix: I wasn't sure if there was a difference between the built-in split and the jquery split.
    – chris
    Apr 9, 2010 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 stackoverflow.com/questions/2790001/…
    – Luis Perez
    Jan 11, 2012 at 2:44

3 Answers 3

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.

1
  • 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 9, 2010 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;
  };
}
1
  • 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. Apr 9, 2010 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;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.