So I'm using jQuery templates and have the following code working in my application:
@using (Html.BeginForm())
{
@Html.DropDownList("StateList",
Model.States,
"< Select >"
)
<div id="designCenters"></div>
<script id="designCenterTemplate" type="text/html">
<p><li>${Name}</li></p>
</script>
}
<script language="javascript" type="text/javascript">
$("#StateList").change(function () {
$.getJSON("/api/designcentersbystate/" + $(this).val(),
function (data) {
$("#designCenterTemplate").tmpl(data).appendTo("#designCenters")
});
});
</script>
Since I'm calling .appendTo in the callback function parameter of the template function, obviously the list of design centers is added to the list. My goal is to REPLACE the content within <div id="designCenters">
with the templated results.
Looking for the most efficient way to do this.
Thanks.
For replacing innerHTML of a div with jquery, html() function is used. After loading the web page, on clicking the button content inside the div will be replaced by the content given inside the html() function.
Use the textContent property to change the text of a div element, e.g. div. textContent = 'Replacement text' . The textContent property will set the text of the div to the provided string, replacing any of the existing content.
To replace a DOM element with the specified HTML or DOM elements using jQuery, use the replaceWith() method. The replaceWith (content) method replaces all matched elements with the specified HTML or DOM elements. This returns the JQuery element that was just replaced, which has been removed from the DOM.
Change the success function like so:
function (data) {
$("#designCenters").html($("#designCenterTemplate").tmpl(data))
}
Or you could empty the container each time.
$("#designCenters").empty();
$("#designCenterTemplate").tmpl(data).appendTo("#designCenters");
You can replace the html in an element like this:
$("#designCenters").html(YOUR HTML HERE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With