Solved

Coldfusion, Jquery and special characters

Posted on 2012-04-09
7
379 Views
Last Modified: 2012-04-14
I am using jquery to populate a form.   Here is the way that I am doing it:

<script type="text/javascript">
      $(document).ready(function(){
            <cfoutput query="qry_sel_info">
                  $("##notes").val("#notes#");
            </cfoutput>
       });
</script>

The problem I'm running into is special characters.  Users are entering single quotes (which in this case works), double quotes, and hitting <enter> after lines.  How can I fix this so it will show all the special characters without having problems.  

I'm not too good at regular expressions, but I gave it a shot and here is what I've tried so far:

<cfset aNote = #REReplace(admin_note,"([\'\""])","\\\1", "all")#>
$("##notes").val("#anotes#");

This works to a point.  The problem is that in my textarea it shows:
&quot;This is a 'test'&quot;

The single quotes work the way I want, but I don't want &quot;, i actually want the " to show.  Plus I have no idea what to do with a <enter>

Another issue is that I have a TON of fields that this effects.  What is the easiest way to do this without having to use cfset on every field?  I would assume a function...but i'm fairly new to jquery.  

Thanks in advance for your help
0
Comment
Question by:trifecta2k
  • 4
  • 3
7 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 37823640
(no points ..)

I haven't tested it, but I normally use jsStringFormat which properly escapes input for use in javascript.


Plus I have no idea what to do with a <enter>
Depends on the field type.  Most html elements (except maybe textarea) can't display ascii new lines ie chr(10). If you want to preserve/display them, convert them to html line breaks instead ie <br>.  

So maybe something like    

            $("##notes").val( '#jsStringFormat( replace(notes, chr(10), "<br>", "all") )#' );

Another issue is that I have a TON of fields that this effects.

Then you might consider creating a wrapper function, and call it instead.
0
 
LVL 5

Accepted Solution

by:
trifecta2k earned 0 total points
ID: 37823715
Looks like jsStringFormat works by itself.  I don't need the chr(10) stuff.  jsStringFormat already escapes newline.  I guess that's all I was looking for.  Thanks!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37823727
Ok, kewl. I couldn't remember off the top of my head :)
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 5

Author Comment

by:trifecta2k
ID: 37824030
agx..you wrote no points...how do I accept this as a solution without giving you points?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37824128
I think you can accept one of your own comments as an answer.
0
 
LVL 5

Author Comment

by:trifecta2k
ID: 37824187
OK.  I will do that.  Thanks again for all of your help.
0
 
LVL 5

Author Closing Comment

by:trifecta2k
ID: 37845934
agx didn't want points so I selected my own answer as the solution.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question