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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:46:07+00:00 2026-06-08T15:46:07+00:00

I need to pass strings that may contain special characters to a Javascript function

  • 0

I need to pass strings that may contain special characters to a Javascript function as below.

function setEditMode(countryName, countryCode)
{
    document.getElementById("txtCountryName").value=countryName;
    document.getElementById("txtCountryCode").value=countryCode;        
}

<input id="txtCountryName" name="txtCountryName" type="text" maxlength="50"/><br/>

<input id="txtCountryCode" name="txtCountryCode" type="text" maxlength="2"/><br/>               

<input type="button" value="Submit" id="btnEdit" name="btnEdit" onclick="setEditMode('&#039;xx&#039; &#034;yy&#034; # $', '&#034;&#039;');" />

When the button is clicked the two parameters of type string that contain special characters need to be supplied to the setEditMode() Javascript function. The strings need to be escaped. I have tried using the escape() function but it didn’t work. Is there a way to pass such parameters to a function? (I’m dealing with JSP).

[Those parameters of the setEditMode() function correspond to 'xx' "yy" # $ and "'
respectively].

  • 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-06-08T15:46:09+00:00Added an answer on June 8, 2026 at 3:46 pm

    The parameters you show in your sample contains html entities. Are you trying to decode them? There is no native method in Javascript to do that, but you can use the following code snippet (requires jQuery):

     $("<div>").html("&#034;").html();
    

    In your case:

     var countryName = $("<div>").html(countryName).html();
     var countryCode = $("<div>").html(countryCode).html();
    

    Or use a generic function:

    function decodeHtmlEntities(value)
    {
       return $("<div>").html(value).html();
    }
    

    and

    function setEditMode(countryName, countryCode)
    {
        document.getElementById("txtCountryName").value=decodeHtmlEntities(countryName);
        document.getElementById("txtCountryCode").value=decodeHtmlEntities(countryCode);        
    }
    

    Or use this function: http://phpjs.org/functions/html_entity_decode:424
    if you’re not in the mood for creating div tags with jQuery.

    Edit: Based on your comment. Seems to be a problem with entities for " and ' in inline script. You can solve it by calling a method like this:

    <input type="button" value="Submit" id="btnEdit" name="btnEdit" onclick="setEditModeWrapper()" />
    <script type="text/javascript">
        function setEditModeWrapper()
        {
            setEditMode('&#039;xx&#039; &#034;yy&#034; # $', '&#034;&#039;');
        }
    </script>   
    

    And combine this solution with the decoding functions above if you want those ' to be decoded in the input fields.

    Or more classy:

    <input type="button" value="Submit" class="submitBtn" id="btnEdit" name="btnEdit" data-countryname="&#039;xx&#039; &#034;yy&#034; # $" data-countrycode="&#034;&#039;" />
    
    <script tyoe="text/javascript">
    $(".submitBtn").on("click", function(e)
    {
        var btn = $(e.currentTarget);
        var countryName = btn.attr("data-countryname");
        var countryCode = btn.attr("data-countrycode");
        setEditMode(countryName, countryCode);
    });
    </script>
    

    Place the script tag at the bottom of the page and it will handle all your submit buttons based on their data-countryname and data-countrycode attributes. Render them in the same way you render your data in the onclick handler.

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

Sidebar

Related Questions

I have a list of strings that I need to pass to a process
I have a dynamic query string that I need to pass via an .htaccess
I need to pass a list of strings as parameter to a console application
I need to find a way to pass some strings to my word 2003
I have a list of integers or of strings and need to pass it
I need to pass a string literal to a function myfunction(arg1 DEF_CHAR arg1); now
in a xml/non-xml File there may exist some XML Block that I need to
I need to pass the JavaScript tz_offset variable on the client size to a
I need to create a WCF Service that will have a download file function.
So I have a flashobject which I need to pass a formatted DateTime string

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.