Link to home
Start Free TrialLog in
Avatar of asaworker
asaworker

asked on

Issue with emdashes when comparing 2 strings

I'm trying to compare a form value with an array value. It turnsd out the issue is with the unicde equivalent of an emdash — that's stored in the database. Any way to convert it on the client side, or any other workarounds.

The code is below:

<html>
<script>

 
 

      //STORE THE Product and its COST in an array, You can generate this section using your Server script
       var arProductA=new Array();
        
              arProductA[0]=new Array(3);
            arProductA[0][0]="TESTA";
            arProductA[0][1]="Boston";
            arProductA[0][2]="Massachusetts";
        
              arProductA[1]=new Array(3);
            arProductA[1][0]="Embassy Suites&#8212;Nashville-South/Cool Springs";
            arProductA[1][1]="Franklin";
            arProductA[1][2]="Tennessee";
        
              arProductA[2]=new Array(3);
            arProductA[2][0]="Embassy Suites&#8212;San Francisco";
            arProductA[2][1]="South San Francisco";
            arProductA[2][2]="California";
        
              arProductA[3]=new Array(3);
            arProductA[3][0]="Embassy Suites&#8212;Santa Ana";
            arProductA[3][1]="Santa Ana";
            arProductA[3][2]="California";
        
              arProductA[4]=new Array(3);
            arProductA[4][0]="Grand Canyon University";
            arProductA[4][1]="Phoenix";
            arProductA[4][2]="Arizona";
        
              arProductA[5]=new Array(3);
            arProductA[5][0]="Holiday Inn&#8212;Buena Park Hotel & Conference Center";
            arProductA[5][1]="Buena Park";
            arProductA[5][2]="California";
        
              arProductA[6]=new Array(3);
            arProductA[6][0]="Long Island Marriott Hotel & Conference Center";
            arProductA[6][1]="Uniondale";
            arProductA[6][2]="New York";
        
              arProductA[7]=new Array(3);
            arProductA[7][0]="Pima Community College Community Campus";
            arProductA[7][1]="Tucson";
            arProductA[7][2]="Arizona";
        
              arProductA[8]=new Array(3);
            arProductA[8][0]="Sherwood Inn";
            arProductA[8][1]="Skaneateles";
            arProductA[8][2]="New York";
        
              arProductA[9]=new Array(3);
            arProductA[9][0]="University of Alaska";
            arProductA[9][1]="Anchorage";
            arProductA[9][2]="Alaska";
        
              arProductA[10]=new Array(3);
            arProductA[10][0]="University of Phoenix&#8212;Hohokam Campus";
            arProductA[10][1]="Phoenix";
            arProductA[10][2]="Arizona";
            
            function populate(oCbo)
{
      var oProdA = document.theForm.venue;
      var oCostA = document.theForm.location;
        var oCost2A = document.theForm.location_state;
     
       if (oProdA.selectedIndex !=0)
      {

            //SHOW THE COST
            for(var j=0; j<arProductA.length; j++){
                  if(oProdA.value==arProductA[j][0])
                  {
                        oCostA.value = arProductA[j][1];
                                    oCost2A.value = arProductA[j][2];
                  }
            }
      }
      else
      {
            oCostA.value = "";
                  oCost2A.value = "";
      }

  }
        
</script>


<body>

  <form name="theForm">
     <select name="venue" onChange="populate();">
        <option value="">&#8212;Select&#8212;</option>
        <option value="TESTA">TESTA</option>
                                                      
                                                      <option class="state" value="Embassy Suites&#8212;Nashville-South/Cool Springs">Embassy Suites&#8212;Nashville-South/Cool Springs</option>
                                                      
                                                      <option class="state" value="Embassy Suites&#8212;San Francisco">Embassy Suites&#8212;San Francisco</option>
                                                      
                                                      <option class="state" value="Embassy Suites&#8212;Santa Ana">Embassy Suites&#8212;Santa Ana</option>
                                                      
                                                      <option class="state" value="Grand Canyon University">Grand Canyon University</option>
                                                      
                                                      <option class="state" value="Holiday Inn&#8212;Buena Park Hotel & Conference Center">Holiday Inn&#8212;Buena Park Hotel & Conference Center</option>
                                                      
                                                      <option class="state" value="Long Island Marriott Hotel & Conference Center">Long Island Marriott Hotel & Conference Center</option>
                                                      
                                                      <option class="state" value="Pima Community College Community Campus">Pima Community College Community Campus</option>
                                                      
                                                      <option class="state" value="Sherwood Inn">Sherwood Inn</option>
                                                      
                                                      <option class="state" value="University of Alaska">University of Alaska</option>
                                                      
                                                      <option class="state" value="University of Phoenix&#8212;Hohokam Campus">University of Phoenix&#8212;Hohokam Campus</option>
                                                      
                                   
     </select>
      <input type="text" name="location" value=""><br>
      <input type="text" name="location_state" value="">

  </form>
</body>
</html>
Avatar of Scott Bennett
Scott Bennett
Flag of United States of America image

The problem is that the browser will resolve the &#8212; characters before they are sent to the javascript function.

You can see this if you add an alert to dump out the two strings being compared withing your loop:

 //SHOW THE COST
            for(var j=0; j<arProductA.length; j++){
                        alert(oProdA.value + "\n" + arProductA[j][0]);
                  if(oProdA.value==arProductA[j][0])
                  {
                        oCostA.value = arProductA[j][1];
                                    oCost2A.value = arProductA[j][2];
                  }
            }


The easiest way to resolve this is to use your server side scripting language to replace &#8212; with  when you are creating the javascript array so that your array source looks like:
//STORE THE Product and its COST in an array, You can generate this section using your Server script
       var arProductA=new Array();
       
              arProductA[0]=new Array(3);
            arProductA[0][0]="TESTA";
            arProductA[0][1]="Boston";
            arProductA[0][2]="Massachusetts";
       
              arProductA[1]=new Array(3);
            arProductA[1][0]="Embassy SuitesNashville-South/Cool Springs";
            arProductA[1][1]="Franklin";
            arProductA[1][2]="Tennessee";
       
              arProductA[2]=new Array(3);
            arProductA[2][0]="Embassy SuitesSan Francisco";
            arProductA[2][1]="South San Francisco";
            arProductA[2][2]="California";
       
              arProductA[3]=new Array(3);
            arProductA[3][0]="Embassy SuitesSanta Ana";
            arProductA[3][1]="Santa Ana";
            arProductA[3][2]="California";
       
              arProductA[4]=new Array(3);
            arProductA[4][0]="Grand Canyon University";
            arProductA[4][1]="Phoenix";
            arProductA[4][2]="Arizona";
       
              arProductA[5]=new Array(3);
            arProductA[5][0]="Holiday InnBuena Park Hotel & Conference Center";
            arProductA[5][1]="Buena Park";
            arProductA[5][2]="California";
       
              arProductA[6]=new Array(3);
            arProductA[6][0]="Long Island Marriott Hotel & Conference Center";
            arProductA[6][1]="Uniondale";
            arProductA[6][2]="New York";
       
              arProductA[7]=new Array(3);
            arProductA[7][0]="Pima Community College Community Campus";
            arProductA[7][1]="Tucson";
            arProductA[7][2]="Arizona";
       
              arProductA[8]=new Array(3);
            arProductA[8][0]="Sherwood Inn";
            arProductA[8][1]="Skaneateles";
            arProductA[8][2]="New York";
       
              arProductA[9]=new Array(3);
            arProductA[9][0]="University of Alaska";
            arProductA[9][1]="Anchorage";
            arProductA[9][2]="Alaska";
       
              arProductA[10]=new Array(3);
            arProductA[10][0]="University of PhoenixHohokam Campus";
            arProductA[10][1]="Phoenix";
            arProductA[10][2]="Arizona";
The removed the characters I am trying again in the code snippet to see if that works
//STORE THE Product and its COST in an array, You can generate this section using your Server script
       var arProductA=new Array();
        
              arProductA[0]=new Array(3);
            arProductA[0][0]="TESTA";
            arProductA[0][1]="Boston";
            arProductA[0][2]="Massachusetts";
        
              arProductA[1]=new Array(3);
            arProductA[1][0]="Embassy SuitesNashville-South/Cool Springs";
            arProductA[1][1]="Franklin";
            arProductA[1][2]="Tennessee";
        
              arProductA[2]=new Array(3);
            arProductA[2][0]="Embassy SuitesSan Francisco";
            arProductA[2][1]="South San Francisco";
            arProductA[2][2]="California";
        
              arProductA[3]=new Array(3);
            arProductA[3][0]="Embassy SuitesSanta Ana";
            arProductA[3][1]="Santa Ana";
            arProductA[3][2]="California";
        
              arProductA[4]=new Array(3);
            arProductA[4][0]="Grand Canyon University";
            arProductA[4][1]="Phoenix";
            arProductA[4][2]="Arizona";
        
              arProductA[5]=new Array(3);
            arProductA[5][0]="Holiday InnBuena Park Hotel & Conference Center";
            arProductA[5][1]="Buena Park";
            arProductA[5][2]="California";
        
              arProductA[6]=new Array(3);
            arProductA[6][0]="Long Island Marriott Hotel & Conference Center";
            arProductA[6][1]="Uniondale";
            arProductA[6][2]="New York";
        
              arProductA[7]=new Array(3);
            arProductA[7][0]="Pima Community College Community Campus";
            arProductA[7][1]="Tucson";
            arProductA[7][2]="Arizona";
        
              arProductA[8]=new Array(3);
            arProductA[8][0]="Sherwood Inn";
            arProductA[8][1]="Skaneateles";
            arProductA[8][2]="New York";
        
              arProductA[9]=new Array(3);
            arProductA[9][0]="University of Alaska";
            arProductA[9][1]="Anchorage";
            arProductA[9][2]="Alaska";
        
              arProductA[10]=new Array(3);
            arProductA[10][0]="University of PhoenixHohokam Campus";
            arProductA[10][1]="Phoenix";
            arProductA[10][2]="Arizona";

Open in new window

Avatar of asaworker
asaworker

ASKER

I figured that was the issue after looking at it for awhile. Do you have an example using coldfusion?
I guess not.... Just make sure to replace the &#8212; with the actual emdash character when you are generating the array javascript
something like this:
arProductA[4][0]="#replace(myquery.hotelname,'##8212;',chr(8212),"ALL")#";
ASKER CERTIFIED SOLUTION
Avatar of Scott Bennett
Scott Bennett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ANy idea how to add another zone for this question?
I don't know... probably community support
Perfect, this worked. Any idea how to add 8217 to it as well?

This is what worekd: arLocationA[#i#][0]="#replace(locationname,'&##8212;',chr(8212),'ALL')#";
#replace(#replace(locationname,'&##8212;',chr(8212),'ALL')#,'&##8217;',chr(8217),'ALL')#
oops sorry too many #'s should be:
#replace(replace(locationname,'&##8212;',chr(8212),'ALL'),'&##8217;',chr(8217),'ALL')#
Thanks SBenmnet, this is good to know, I appreciate all your help.