I am trying to pass strings with spaces and special characters, but its getting error and nothing is working.
e.g.
<img onclick='addKeyword("celebritynews&gossip");' src="images/plus.png" >
but the ‘&’ inside string celebritynews&gossip is breaking the code.
I figured out that the problem is in addKeyword function, I’d used the following code for defining a variable named url,
function addKeyword(categoryId){
var url = "addKeyword.php?uid=" + user_id + "&categoryId=" + categoryId + "&keyword=" + $("#keyword_" + categoryId).attr("value");
}
and that $(“#keyword_” + categoryId) is causing problem. Any way to solve that?
Assuming you are passing the string on a URL, the parameter values need to be URL encoded.
…SUMMARY EDIT…
The
&is interpreted as HTML and must be HTML encoded, specifically&HTML
JavaScript
Use encodeURIComponent(s).