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

  • Home
  • SEARCH
  • 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 3988726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:17:12+00:00 2026-05-20T06:17:12+00:00

Ive got a really strange problem with some javascript code that ive written. I

  • 0

Ive got a really strange problem with some javascript code that ive written. I have set this code to execute on load. The idea of it is to remove a value from a text field if it is listed in another field. Here is the code:

var diseases = document.getElementById("AffectedBy").value;  
        var diseasearray = diseases.split("-");  
        alert("disease array size: " + diseasearray.length);  
        for (i=0;i<diseasearray.length;i++)  
            if (diseases.match("-" + diseasearray[i]))  
                {  
                    diseasearray[i] = "-" + diseasearray[i];  
                    alert(diseasearray[i]);                           
                    document.getElementById("DiseaseNotSelected").value=document.getElementById("DiseaseNotSelected").value.replace(diseasearray[i],"")
                }

This code above gives an array size of 3 (1 blank, and 2 values) but when I display the values in the alert it only shows 2 values(1 blank, and 1 value)

This piece of code:

var foods = document.getElementById("FoodFor").value;  
        var foodarray = foods.split("-");  
        alert("food array size: " + foodarray.length);  
        for (i=0;i<foodarray.length;i++)  
            if (foods.match("-" + foodarray[i]))  
                {  
                    foodarray[i] = "-" + foodarray[i];  
                    alert(foodarray[i]);  
                    document.getElementById("FoodNotSelected").value=document.getElementById("FoodNotSelected").value.replace(foodarray[i],"")
                }

This code above gives an array size of 3 (1 blank, and 2 values) and when I display the values in the alert it only shows 3 values(1 blank, and 2 value).

Can anyone see a reason why the first code block only shows 2 items in the array as I cant see why and its really bugging me now.

  • 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-05-20T06:17:13+00:00Added an answer on May 20, 2026 at 6:17 am

    As I said in my comment above: beware that whatever you pass into match will get turned into a regular expression. If that contains some special character, it might not match where you would expect it to (or vice versa).

    The string White Spot (Ich) will be turned into the regex /White Spot (Ich)/; which does not match White Spot (Ich) but does match White Spot Ich, since the parentheses are grouping operators in a regex.

    Change the regular expression test

    diseases.match("-" + diseasearray[i])
    

    into the plain string comparison

    diseases.indexOf("-" + diseasearray[i]) !== -1
    

    and you should be set.

    (I think. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a very strange problem, well not really a problem because i've fixed
This case is really strange, i've spent 2 whole days to get Twitter Oauth
Hey all! Ive got timezone troubles. I have a time stamp of 2010-07-26 23:35:03
I've got a really weird problem whereby I when I click on a UITextField
This question comes from a range of other questions that all deal with essentially
I've got a Canvas and I put some stuff on it, then I scaled
Ive got a bit stuck figuring it out for the negative direction? it must
I've got a core data app with 2 views. The first view lists Rooms,
At top there is some most useful info about foo. Between horizontal lines there
I've run into a bit of a funny one and thought it was worth

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.