I have an element that gets generated dynamically:
// gathers the name of the file the user is uploading
let fileName = escape(e.target.files[0].name).slice(0,9);
let icon = $(` // this extra space is for readability on SO
<div class="fileImage" id="${fileName}">
<div class="glyph-stack">
<i class="glyphicon glyphicon-file file-icon"></i>
<i class="glyphicon glyphicon-file file-icon overlay"></i>
<i class="glyphicon glyphicon-remove file-icon" onclick="removeFile(${fileName})"></i>
</div>
<span class="file-title">${fileName}</span>
</div>`);
When I append the element to the DOM and inspect it, all instances of ${fileName} appear correctly. If a file named freeicons.zip is uploaded, "freeicons" appears everywhere I placed ${fileName}. However, once the removeFile function is called in the onclick handler:
function removeFile(fileName){
$('#'+fileName).remove();
}
The variable fileName inside the function no longer equals "freeicons". When I inspect it, the variable is set to div#freeicons.fileImage, which is the selector for the entire element.
If, in the onclick handler call to removeFile() I wrap ${fileName} in single quotes:
`onclick="removeFile('${fileName}')"`
I get the string "freeicons" inside removeFile().
Why in the first case does it replace the string with the element selector? Is this something JQuery is doing when it creates this little element tree?