I have the following HTML code stored as a variable:
<body>
<div>
<table>
<field id='Contact Name' required>
<exists>$value<br></exists>
<notexists></notexists>
</field>
<field id='Job Role' required>
<exists>$value<br></exists>
<notexists></notexists>
</field>
</table>
</div>
</body>
</html>
Now, what I'm trying to do is access this code and change the value of $value for each field based on inputs within my form and then render the final html inside of an iFrame.
Here is the JavaScript code I am using to do this:
function renderTemplate() {
var template = $(selectedTemplate.html).contents(); //HTML code I have given above.
$("input").each(function(index, input) {
var element = $(input); //<input type="text" ref="Contact Name">
if (element.attr("ref")) {
var reference = element.attr("ref");
var field = template.find("field[id='" + reference + "']");
if (typeof(field) != undefined) {
var exists = field.find("exists");
var notExists = field.find("notexists");
if (typeof(exists) != undefined && typeof(notexists) != undefined) {
if (element.val()) {
exists.html(exists.html().replace("$value", element.val()));
} else {
exists.html(notExists.html());
}
$('#signaturePreview').contents().find('html').html(template.html());
}
}
}
});
}
Now for whatever reason, it cannot find the fields with the selector I'm giving.
I did some testing with the chrome console, and it can't find any fields at all, even when I use:
template.find("field") // Gives 0 results.