Derek
Derek

Reputation: 2112

Weird indexOf issue

Going to be very hard to explain this without hundreds of lines of code but I will try!

I have a parent object which contains an array of children objects. The parent writes a MULTIPLE select element to a page using this function

function writecollectionmultiselect(name,displayname,selectedrange){    

// Create select page element
var collection = $("<select/>");
collection.attr("name",name);
collection.attr("MULTIPLE","multiple");

// Loop over elements in this collection and ask them to draw themselves
for(var i=0; i<this.objectarray.length;i++){
    collection.append(this.objectarray[i].writemultioption(selectedrange));
}
return collection;
}

The parent asks each child to draw its own option element depending on whether their id is in the 'selectedrange'

function writemultioption(selectedrange){
    var option = $("<option/>");
    option.val(this.data.id);
option.html(this.data.name);
if(selectedrange.indexOf(parseInt(this.data.id)) >= 0){
    option.attr('selected', 'selected');
}
return option;
}

This works fine when selectedrange is provided as selectedrange=[1,2,3] However if I read the selected values of the page element using jquery

selectedrange = $('[name='+myname+"]").val();

when I try to call the first function the indexOf function seems to completely fall over. I added this line of debug code

alert("looking for "+this.data.id+" in "+selectedrange+" result "+selectedrange.indexOf(parseInt(this.data.id)))

When I first draw the selector I get this:

looking for 1 in 1,2,3 result 0

However, reading the value and redrawing gives

looking for 1 in 1,2,3 result -1

I suspect some sort of data type issue but hvae been banging my head against this for hours. Any help appreciated.

Upvotes: 1

Views: 268

Answers (2)

Hunter McMillen
Hunter McMillen

Reputation: 61515

The indexOf() function takes a String as a parameter, in your example about you pass in an integer, try not calling parseInt():

alert("looking for " + this.data.id + " in " + selectedrange + "result" + selectedrange.indexOf(this.data.id))
                                                                                      // no parseInt() here

Also, the reason you get -1 is because the indexOf() function returns -1 when it finds no match in the String it is searching.

Upvotes: 0

gray state is coming
gray state is coming

Reputation: 2107

jQuery will give you an array of strings, and you are converting the this.data.id to a number using parseInt so they will never match, because the indexOf comparison is strict.

One solution is to convert the array of strings to numbers.

selectedrange = $.map($('[name='+myname+"]").val(), function(n) { 
                                                        return parseInt(n, 10);
                                                    });

Or another solution is to keep the array as strings, but get rid of the parseInt and just do this.

if(selectedrange.indexOf(this.data.id) >= 0){

Upvotes: 1

Related Questions