function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anil Kumar DevarapuAnil Kumar Devarapu 

How to replace ' &amp ' with ' & ' in Jquery

Hi There, 

I am using Auto complete component from here http://goo.gl/NMpwnH

I have Special Characters in My record name for Accouunt and Other objects, 

when i focused or Selected the Names with Special Characters as encoded form, It displaying ' & ' as ' &amp ' 
I have posted my component Code below
please Anyone suggest me to  replace ' &amp ' as ' & '

VF Component
<apex:component controller="autoCompleteController">


   <apex:includeScript value="{!URLFOR($Resource.jqueryui18, 'js/jquery-1.7.1.min.js')}" /> 

   <apex:includeScript value="{!URLFOR($Resource.jqueryui18, 'js/jquery-ui-1.8.18.custom.min.js')}" /> 

   <apex:stylesheet value="{!URLFOR($Resource.jqueryui18, 'FlickTheme.css')}"/> 

  

  <!-- Attributes Required For Component -->

  <apex:attribute name="objectname" description="The object name you want to look for." type="String" required="true"/>

  <apex:attribute name="additionalfield" description="Any additional fields you'd like to search and include in the display." type="String" required="false"/>

  <apex:attribute name="profilename" description="To filter on the basis of profile name and include in the display." type="String" required="false"/>

  <apex:attribute name="autocomplete_textbox" description="The ID for the Autocomplete List Textbox." type="String" required="true"/>

   <!-- CSS  -->

  <style>

    .ui-autocomplete-loading {background: white url({!$Resource.loadingIcon}) right center no-repeat;}

  </style>

  <!-- Javascript -->

  <script type="text/javascript">

    var j$ = jQuery.noConflict();

    j$(document).ready(function() 

    {

        var sObjects;

        var queryTerm;

        j$(esc('{!autocomplete_textbox}')).autocomplete({

            minLength: 1,

            source: function(request, response) {

                        queryTerm = request.term;

                        autoCompleteController.findSObjects("{!objectname}", request.term, "{!additionalfield}", "{!profilename}", function(result, event){

                            if(event.type == 'exception') 

                            {

                                  alert(event.message);

                            } else 

                            {

                                 sObjects = result;

                                 response(sObjects);

                            }

                        });

                   },

            focus: function( event, ui ) {

                    
                    j$(esc('{!autocomplete_textbox}')).val(ui.item.Name);
                    
                    
                    return false;

                    },

            select: function( event, ui ) {

                        j$(esc('{!autocomplete_textbox}')).val( ui.item.Name );

                        j$(esc('{!autocomplete_textbox}_lkid')).val( ui.item.Id );

                       j$(esc('{!autocomplete_textbox}_lkold')).val( ui.item.Name );

                        if (event.keyCode == 13) { 

                           event.preventDefault();

                        }

                        return false;

                    },
                    

                    
                    

         })

         .data( "autocomplete" )._renderItem = function( ul, item ) {

            var entry = item.Name;

            if("{!additionalfield}" !='')

            {

                j$.each("{!additionalfield}".split(",") , function(key, value) {

                    entry = entry + " " + item[value];

                });

            }

            //entry = entry + "</a>";

            //entry = entry.replace(queryTerm, "<b>" + queryTerm + "</b>");

            entry = entry.replace( new RegExp( "(" + queryTerm + ")" , "g" ), "<strong>$1</strong>" );

            return j$( "<li></li>" )

                .data( "item.autocomplete", item )

                .append( "<a>" + entry + "</a>")

                .appendTo( ul );

        };

    });

    function esc(myid) 
    {
         return '#' + myid.replace(/(:|\.\&)/g, '\\\\$1');

           //return '#' + myid.replace(/&amp;/g, '&')
    }


  </script>

</apex:component>

Component Controller
 
global class autoCompleteController 
{
    @RemoteAction
    global static SObject[] findSObjects(string obj, string qry, string addFields, string profilename) 
    {
        /* More than one field can be passed in the addFields parameter
           Split it into an array for later use */
        List<String> fieldList=new List<String>();
        if (addFields != '')  
        fieldList = addFields.split(',');
        
        /* Check whether the object passed is valid */
        Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
        Schema.SObjectType sot = gd.get(obj);
        if (sot == null) 
        {
            return null;
        }
        
        /* Creating the filter text */
        String filter = ' like \'%' + String.escapeSingleQuotes(qry) + '%\'';
        
        /* Begin building the dynamic soql query */
        String soql = 'SELECT Name';
        
        /* If any additional field was passed, adding it to the soql */
        if (fieldList.size()>0) 
        {
            for (String s : fieldList) 
            {
                soql += ', ' + s;
            }
        }
        
        /* Adding the object and filter by name to the soql */
        soql += ' from ' + obj + ' where name' + filter;
        
        if(profilename!='')
        {
            //profile name and the System Administrator are allowed
            soql += ' and Profile.Name like \'%' + String.escapeSingleQuotes(profilename) + '%\'';
            system.debug('Profile:'+profilename+' and SOQL:'+soql);
        }
        
        /* Adding the filter for additional fields to the soql */
        if (fieldList != null) 
        {
            for (String s : fieldList) 
            {
                soql += ' or ' + s + filter;
            }
        }
        
        soql += ' order by Name limit 20';
        
        system.debug('Qry: '+soql);
        
        List<sObject> L = new List<sObject>();
        try 
        {
            L = Database.query(soql);
        }
        catch (QueryException e) 
        {
            system.debug('Query Exception:'+e.getMessage());
            return null;
        }
        return L;
   }
}

Usage : 
 
<apex:inputText label="Name :" value="{!name}" id="Text" required="true" html-autocomplete="on" html-PlaceHolder="Name.." html-inputtype="text" > 
 <c:AutoCompleteComponent autocomplete_textbox="{!$Component.Text}" objectname="Account" /> 
</apex:inputtext>

ScreeenShot of Autocomplete
Screenshot of AutoSelect

 
Best Answer chosen by Anil Kumar Devarapu
Sachin Chavan 27Sachin Chavan 27
Hi Anil,

No need to escape charcters with Remote Action methods if you pass options  { escape: false } as a last parameter to remote action method call.
In above example, modify remote method call as follows.

autoCompleteController.findSObjects("{!objectname}", request.term, "{!additionalfield}", "{!profilename}", function(result, event){
...
}, { escape: false });

All Answers

Sachin Chavan 27Sachin Chavan 27
Hi Anil,

No need to escape charcters with Remote Action methods if you pass options  { escape: false } as a last parameter to remote action method call.
In above example, modify remote method call as follows.

autoCompleteController.findSObjects("{!objectname}", request.term, "{!additionalfield}", "{!profilename}", function(result, event){
...
}, { escape: false });
This was selected as the best answer
Anil Kumar DevarapuAnil Kumar Devarapu
Hi Sachin, first of all, Thank you so much for your Kind reply. I thought I cant resolve this. You saved my Lot of time truly. Thank you So much:)