Lasang Lasang - 7 months ago 24
Javascript Question

Passing string with space character as parameter to on-click function generated by javascript

My hypertlink generated by JavaScript is as follows:


'<a href="#" onClick=passSearchTerm("' +aa + '");>View</a>'



The parameter passes wells when the string is as follows:


var aa = 'TAGS(\\"ab\\")';



I confirmed it by alerting inside the function


function passSearchTerm(aa)
{
alert(aa);
}


However, this wont alert when the var is defined as follows:


var aa = 'TAGS(\"a b\")'; //space between "a" and "b"


Can anyone suggest me how to pass such string? Thanks in advance for your help.
Actually I am trying to pass likely following string:


TAGS("a or b")

Answer

While generating ur hyperlink by javascript use encodeURIComponent ex.

"<a href=\"#\" onclick=\"passSearchTerm('" + encodeURIComponent(aa) + "')\">View</a>"

and while retriving use decodeURIComponent

function passSearchTerm(aa) { alert(decodeURIComponent(aa)); }
Comments