xxintxx
xxintxx

Reputation: 1

jQuery encoding for special characters

I am working on a web application in ASP.NET MVC I have following parts of code in my HTML:

<script>
$(function () {
    $('.foo').click(function () {
        var fooName = $("#fooName").val();
        this.href = this.href.replace("xxx", fooName);
    });
});

and:

<input type="text" value="@ViewBag.fooName" name="fooName" id="fooName" autocomplete="on" />

and:

@Html.ActionLink("Action", "ActionName", new { someObject = item.ID, fooName="xxx"}, new { @class = "foo" })

it works fine if there are no special characters (like #) in the textBox input. Otherwise it messes it up. I need those special characters though. I have found a tip: Javascript/jQuery encoding for special characters when using val() but I don't know how to use it if I am declaring the variable fooName. I have no idea about JavaScript, I have never seen it before. Please help me make it work.

Upvotes: 0

Views: 235

Answers (1)

James
James

Reputation: 1446

Use encodeURIComponent(). encodeURIComponent() escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )

Source: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

Upvotes: 1

Related Questions