3

I have a function that returns values from a database. My issue: the function adds "" on the return values. I need to trim the values before displaying them on the user's screen.

This is my function

 <script language="JavaScript" type="text/javascript">
    function handleProcedureChange(procedureid)
    {
        procedureid= document.form1.procedure.value;
        //alert(procedureid);
        var url ="some URL goes here"; 
        url=url+"ProcedureID="+procedureid;

        $.get(url, function(procedureResult) {
            $("#procedureDescription").text(procedureResult);
        });
    }
   </script>

The prcedureResult is the value being returned and the one I need to trim from the quotes before displaying it.

Geo
  • 3,160
  • 6
  • 41
  • 82
  • 3
    Fix the problem at the root - dont mess around removing the quotes in JavaScript – Manse May 15 '12 at 16:12
  • I don't see you useing any trim function, so what problem are you having with it? – Felix Kling May 15 '12 at 16:12
  • @ManseUK You are right my friend. I will edit my question and add more tags.Thanks – Geo May 15 '12 at 16:22
  • @FelixKling I need to use a trim function – Geo May 15 '12 at 16:23
  • @ManseUK your version works with the fix by Rocket. Thanks – Geo May 15 '12 at 16:25
  • Have you tried anything or searched at least? There are many related questions, like [How do I trim a string in javascript?](http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript), [Javascript: How to remove characters from end of string?](http://stackoverflow.com/questions/3597611/javascript-how-to-remove-characters-from-end-of-string), [how to remove “,” from a string in javascript](http://stackoverflow.com/questions/908608/how-to-remove-from-a-string-in-javascript),... – Felix Kling May 15 '12 at 16:42
  • @FelixKling I did on other forums but not here. Thanks for the links. They will come handy in the future. – Geo May 15 '12 at 17:28

2 Answers2

3

Use this to remove quotes :

$.get(url, function(procedureResult) {
     procedureResult = procedureResult.replace(/^"+|"+$/g, "");
     $("#procedureDescription").text(procedureResult);
});

This replaces " from the start (^) and end ($) of the string with nothing ("")

Manse
  • 37,765
  • 10
  • 83
  • 108
3

Try the following function which will strip off all leading and trailing quotes from a string value

function stripQuotes(str) {
  str = str.replace(/^\"*/, '');
  str = str.replace(/\"*$/, '');
  return str;
}

It can be used as so

$.get(url, function(procedureResult) {
  procedureResult = stripQuotes(procedureResult);
  $("#procedureDescription").text(procedureResult);
});
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Thanks for the reply. I used the code on the previews answer and it worked fine. – Geo May 15 '12 at 16:26