I have a JSON object with jQuery selectors and values inside it. Special characters inside the selectors have (I think) been properly escaped. Here is what the JSON looks like:
var config = [{
"fieldname": "First name",
"selector":"\"[id$='txtFirstName']\"",
"value": "Bob"
},
{
"fieldname": "Last name",
"selector": "\"[id$='txtLastName']\"",
"value": "Smith"
}
];
I have code that attempts to walk through this data, and apply the relevant value to the control indicated by the selector. That code looks like this:
$(config).each(
function() {
console.log('selector:')
console.log(this.selector);
var element = $(this.selector);
element.val(this.value);
}
)
When that code runs, I get this output:
"selector:"
"\"[id$='txtFirstName']\""
Followed by this error:
Error: Syntax error, unrecognized expression:
\"[id$='txtFirstName'</a>\""
It looks like the double-quotes are being encoded (again) as the selector is pulled out. Why would that be? And how can I prevent it?
selector
property."selector":"[id$='txtFirstName']",
$(""[id$='txtLastName']"");
which is clearly rubbish. Your selector should not be surrounded with"
- fix the selector:"selector": "[id$='txtFirstName']",
$("[id$='txtLastName']")
. The answer from @Hardik Uchdadiya resolved the issue.