I am trying to change the class of one or more elements within a template when a button is clicked. The samples I have ssen all usually find one item with .tmplItem, and I can’t quite figure out how to achieve what I’m trying to do. This is more or less a proof of concept.
i have a structure like below.
var messages = [
{ Body: "Testing 1", Read: "0" },
{ Body: "Testing 2", Read: "1" },
{ Body: "Testing 3", Read: "0" },
{ Body: "Testing 4", Read: "1" }
];
my template is:
<script id="messageTemplate" type="text/html">
{{if Read == "1"}}
<tr class="hdnMessage">
<td class="hdnMessage" >
<input type="checkbox" id="cbHasReadMessage" checked="checked">
</td>
{{else}}
<tr class="showMessage">
<td><input type="checkbox" id="cbHasReadMessage"></td>
{{/if}}
<td>${Body}</td>
</tr>
</script>
css is:
<style type="text/css">
.hdnMessage
{
display:none;
}
.showMessage
{
display:block;
}
</style>
I have a button outside of the template
<input type="button" value="Show Read Messages" id="btnShowHideReadMessages" />
which I added a click event for:
$("#btnShowHideReadMessages").click(function() {
if (showingReadMessages) {
showingReadMessages = false;
$(this).val("Show Read Messages");
//try to find checked checkboxes and set the class of the tr which is its parent
}
else {
showingReadMessages = true;
///Try finding all the current hidden TRs
var hdnMessages = $("tr.hdnMessage").tmplItem();
var hdnMsg = hdnMessages.data;
var hdnElement = hdnMessages.nodes;
$(hdnElement).replaceWith($('.showMessage'));
}
});
The last script segment is incomplete as I couldnt figure out how to get this to work. Any help would be appreciated.
Remove
class="showMessage"from the template altogether then change the bottom section of code to something like this: