The main problem when collecting html string with attributes is the placement and escaping of quotes.
To find out why the "did not work" screening in this case, you just need to see the resulting string.
"<span id='crfield' contenteditable='false' ondblclick = 'clickTree(activeControl, \'" + response.res[j] + "\')' class='table_list'>" + response.res[j] + "</span>";
Let the value of response.res[j] be test , then we get the string
<span id='crfield' contenteditable='false' ondblclick = 'clickTree(activeControl, 'test')' class='table_list'>test</span>
As you can see from the result, we have the same quotes in the ondblclick attribute.
What happened here?
The quotes were escaped, but, they were escaped for the string being collected.
Thus, the solution can be to replace quotes with double ("), or screening with the expectation that the resulting string will also be processed
"<span id='crfield' contenteditable='false' ondblclick = 'clickTree(activeControl, \\'" + response.res[j] + "\\')' class='table_list'>" + response.res[j] + "</span>";
As a result, we get
<span id='crfield' contenteditable='false' ondblclick = 'clickTree(activeControl, \'test\')' class='table_list'>test</span>
and as you can see now in the string for the ondblclick attribute ondblclick single quotes are also escaped.
Double shielding did not help, but replacing quotes with special html characters, for example, for single quotes - '
"<span id='crfield' contenteditable='false' ondblclick = 'clickTree(activeControl, '" + response.res[j] + "')' class='table_list'>" + response.res[j] + "</span>";
Working example:
var response = { res: { 0: 'test' } }, j = 0; document.body.innerHTML = "<span id='crfield' contenteditable='false' ondblclick = 'clickTree(activeControl, '" + response.res[j] + "')' class='table_list'>" + response.res[j] + "</span>"; var activeControl = "Check"; function clickTree(a, b) { document.getElementById('crfield').textContent += " " + [a, b]; }
To avoid such problems with screening can work with objects, not a string, for example
var span = document.createElement('span'); span.id = "crfield"; span.contenteditable = false; span.ondblclick = function(){ clickTree(activeControl, response.res[j]);}; span.className = "table_list"; field_td.appendChild(span);
Example:
var response = { res: { 0: 'test' } }, j = 0; var span = document.createElement('span'); span.id = "crfield"; span.contenteditable = false; span.textContent = response.res[j]; span.ondblclick = function() { clickTree(activeControl, response.res[j]); }; span.className = "table_list"; document.body.appendChild(span); var activeControl = "Check"; function clickTree(a, b) { document.getElementById('crfield').textContent += " " + [a, b]; }
" ' \" \' foo \' \" ' "- lexxl