5

I have a requirement where I create radio buttons dynamically based upon JSON response. So far what I did works in Chrome and firefox, but gives Object doesn't support this property or method on if(subItem[1].indexOf(",") >= 0) line

My code

$("#sList").live("change", function(){
    var currentService=this.value;
    var c1Svc=[];
    var c2Svc=[];

    if(absP.length==2)
    {
        $.each(compareServiceData,function(i,item){

            if(currentService==item[0])
            {
                var configCount=0;
                $.each(item[1],function(j,subItem){

                    var temp=subItem[1];

                    /*The JSON response contains List of Lists, so here if it contains a list it will be separated by "," so split it and store in a array, else directly store in a array*/
                    if(subItem[1].indexOf(",") >= 0)
                    {
                        var tList=temp.split(",");
                        $.each(tList,function(k,val){
                            if(configCount==0)
                            {
                                c1Svc.push(val);                                
                            }
                            else
                            {
                                c2Svc.push(val);                                
                            }
                        });
                    }
                    else
                    {
                        if(configCount==0)
                        {
                            c1Svc.push(subItem[1]);                             
                        }
                        else
                        {
                            c2Svc.push(subItem[1]);                             
                        }
                    }
                    configCount++;

                });
            }

        });

        if ($("#customServiceListing").length == 0)
        {               
            $("#compareContent").append('<table id="customServiceListing" align="center" width="90%" class="csm-table" border="1"><tbody><tr><td><form id="c1Service"></form></td><td><form id="c2Service"></form></td></tr></tbody></table>');
        }
        else
        {
            $('#c1Service').empty();
            $('#c2Service').empty();
        }

    }
    else
    {
        $("#compareContent").append('<table align="center" width="90%" class="csm-table" border="1"><tbody><tr><td><form><select id="c1Service"></select></form></td><td><select id="c2Service"></select></td><td><select id="c3Service"></select></td></tr></tbody></table>');
    }


    /*adding service radios to config1*/
    $.each(c1Svc,function(i,item){
        $("#c1Service").append('<input type="radio" name="customConfig1ServiceNames" id="'+item+'" value="'+i+1+'"/>'+item);
    });
    if(c1Svc.length>1)
        $("#c1Service").append('<br/>');

    /*adding service radios to config2*/
    $.each(c2Svc,function(i,item){
        $("#c2Service").append('<input type="radio" name="customConfig2ServiceNames" id="'+item+'" value="'+i+1+'"/>'+item);
    });
    if(c2Svc.length>1)
        $("#c2Service").append('<br/>');
});

Update

Here is a List of various function code not supported by IE8

Update What is the problem here, for every value it gives me -1 I am using code given by Sudhir

alert(subItem[1].indexOf(",")+", "+subItem[1]);

Screenshot

enter image description here

Update

Got it here, var temp=subItem[1].toString(); was the problem, converting it to String worked.

Community
  • 1
  • 1
AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • 1
    Yep, gotta love IE. See here: http://stackoverflow.com/questions/1744310/how-to-fix-array-indexof-in-javascript-for-ie-browsers – McGarnagle Mar 19 '12 at 10:35
  • Yep, gotta love IE. See here: http://stackoverflow.com/questions/1744310/how-to-fix-array-indexof-in-javascript-for-ie-browsers – McGarnagle Mar 19 '12 at 10:36
  • 1
    IE is always years behind all other browsers. This is the second similar question I've seen this morning asking about something not working in IE that works everywhere else. – Rob Mar 19 '12 at 11:55

3 Answers3

16

IE versions < 9 don't have indexOf, so you can add your own:

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;
    };
}

var subItem = [];
subItem[1]="CSMTestPWXListinerService,CSMTestPWXListinerService_ManualyAdded";
console.log(subItem[1].indexOf(","));
//returns 25 
Naftali
  • 144,921
  • 39
  • 244
  • 303
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • @Mef It should be `>>>`. The bitwise [zero-fill right shift](https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators#.3E.3E.3E_(Zero-fill_right_shift) bitwise operator ensures that the `length` value is an integer and positive. – Rob W Mar 19 '12 at 10:40
  • @Rob OK fine, I was mistaking it for escaping, as the whole code was `
    `'d and escaped ^^
    – Leo Mar 19 '12 at 10:57
  • @Sudhir: I used indexOf code from [here](http://stackoverflow.com/questions/2790001/fixing-javascript-array-functions-in-internet-explorer-indexof-foreach-etc) but when I write `alert(subItem[1].indexOf(","));` where `subItem[1]=CSMTestPWXListinerService,CSMTestPWXListinerService_ManualyAdded" it returns `-1` i.e., always it returns `-1`. What is wrong? – AabinGunz Mar 19 '12 at 11:12
  • its working fine for me.. see added code in answer – Sudhir Bastakoti Mar 19 '12 at 11:19
  • update, I am using ur code now, but still I get `-1` for every value – AabinGunz Mar 19 '12 at 11:20
  • @Sudhir: Please see my updated question, whats wrong with it? – AabinGunz Mar 19 '12 at 11:43
  • @Abhishek Simon: Tested in IE6, 7 & 8 and they all seem to work fine with the code – Sudhir Bastakoti Mar 19 '12 at 11:50
  • @Sudhir: but did you see the screenshot I provided, it is somehow giving me `-1` for `CSMTestPWXListinerService,CSMTestPWXListinerService_ManualyAdded` too. Any ideas, where I might be mistaken? – AabinGunz Mar 19 '12 at 11:54
  • Check my update to find out what was the problem. Thanks Sudhir – AabinGunz Mar 19 '12 at 12:35
1

According to http://msdn.microsoft.com/en-us/library/ie/53xtt423(v=vs.94).aspx

MS has said that it support indexof() to IE8 as well!

Vanji
  • 1,696
  • 14
  • 23
  • Well you're looking in the wrong place. [It's not supported.](http://msdn.microsoft.com/en-us/library/ie/ff679977(v=vs.94).aspx) – Bergi Jun 21 '13 at 08:26
  • 1
    So MS Supports indexOf for String but not for Array! isnt it? – Vanji Jun 21 '13 at 08:49
0

What happens if you do this instead?:

if(temp.indexOf(",") >= 0)

I know I've had cases where it seemed to not understand what the object type was when reference from an array instead of a variable created from the contents of that array location. It doesn't make sense that this would be the case, but I've used it as a workaround before.

davidethell
  • 11,708
  • 6
  • 43
  • 63