utf 8 - Properly escape characters in input value in JavaScript -
my goal display sarah o'connor
in input value. need escape sarah o' connor
show correct utf-8 html character.
i'v seen answers suggest replacing each individual special character. surely that's not correct way there hundreds of possible characters in peoples names around world.
i'v tried javascripts escape()
/ unescape()
, encodeuri()
/ decodeuri()
functions, work html text not input values.
please see jsfiddle here: http://jsfiddle.net/ghhza/
var div = $("#my-div"); var input = $("input"); var str = "sarah o'connor"; div.html(str); //--> sarah o'connor input.val(str); //--> sarah o'connor
you can create element put html, can formatted result .text()
, eg:
input.val($("<div>", {html:"sarah o'connor"}).text())
Comments
Post a Comment