2

I need to pass a variable to a JavaScript function, but I have a little trouble. In the .cs file, I have written:

string id = "some'id";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));

As you can see there is an ' (single quote) in the id. Is there any way to work around this issue?

2

4 Answers 4

2

Escape ' with a \ (backslash). For example,

console.log('string with \'');
Sign up to request clarification or add additional context in comments.

Comments

1

Escape your string for such kind of characters"/","\","'"

example

string id = "some/'id";

Comments

0

You should escape your string , which would lead to :

 id = "some\'id";

Comments

0
 <script type="text/javascript">
    function myFunction(someid) {
        someid = someid.replace('#', '\'');
        alert(someid);
    }
</script>

in Your code

string id = "some'id".Replace("'","#");

this.Controls.Add(new LiteralControl("<input type=\"button\" value=\"Test\" onclick=\"myFunction('" + id + "');\">"));

Hope this will Helps you..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.