Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:25:12+00:00 2026-06-09T22:25:12+00:00

Going to be very hard to explain this without hundreds of lines of code

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:25:14+00:00Added an answer on June 9, 2026 at 10:25 pm

    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){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is very hard to explain but I'm going to try. We run a
This is going to be pretty hard to explain, so I'll try to make
It's kind of hard to explain the desired result, but what I was going
Ok, I have been having a VERY hard time figuring this out. I guess
I have a very tricky problem going on with content inside a textarea on
I'm going to implement license key generator on my website but I've very few
I'm going a little mad I think. This is the VERY first thing I
Right, this is a very odd issue that I'm wondering how i am going
I guess this is probably going to be obvious in retrospect but I am
I was thinking about a typical problem that is very JIT-able, but hard to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.