I have a data source that I can not alter, the data source contains a hyperlink with an Id in it that I can use to retrieve data that I need. I created a method to convert the hyperlink to call a javascript function and pass the scrubbed id from the hyperlink, however I am not getting the correct results. In its current state everything is working except the insertion of the id var into the ClickIt method. For this example the value of id is djb137, and when I click on the new hyperlink the error returned is ‘djb137 is undefined. Am I overlooking something on passing a paramater to a javascript function?
private string ScrubHref(string statementHtml)
{
string pattern = "href.*?\"(?<href>.*?)\"";
return Regex.Replace(statementHtml, pattern, delegate(Match match)
{
string v = match.ToString();
string id = Regex.Match(v, "\"([^\"]*?)\"").Groups[1].Value;
string returnValue = "href=\"#\"" + "onclick=\"return ClickIt("+id+");\"";
return returnValue;
});
}
Intrestingly If I change the retrunValue string to this
return v + "onclick=\"return ClickIt(this.href);\"";
Then my javascript function gets the whole href passed to it without issue.But I just need the Id not the whole href.
You need to quote your id as you pass it in to your function call.
The line
Should be
What’s happening is it thinks
djb137is a variable name, think how the method calls would look if you weren’t constructing them in this way. What’s the difference between the following