Issue
I am iterating over my model and I wanted to replace that list with content from same model, only sorted, just by using a button. Let's presume I have this:
<div class="col-md-3">
<button>Replace content </button>
</div
<div class="col-md-9">
{% for c in cats%}
{{c.name}}
{% endfor %}
<p>Content to be replaced</p>
</div>
<div class="col-md-9">
{% for c in animal.cats_set.all %}
{{c.name}}
{% endfor %}
<p>Content to replace above content, sorted by if cat belongs to same animal</p>
</div>
How would I replace the content with the second content in this case ? Im thinking about a jQuery.
Solution
Use the jQuery .replaceWith()
function. Like:
<div class="col-md-3">
<button>Replace content </button>
</div
<div class="col-md-9 old">
{% for c in cats%}
{{c.name}}
{% endfor %}
<p>Content to be replaced</p>
</div>
<div class="col-md-9 new">
{% for c in animal.cats_set.all %}
{{c.name}}
{% endfor %}
<p>Content to replace above content, sorted by if cat belongs to same animal</p>
</div>
Your jQuery function will be:
$(function() {
$(":button").click(function(e) {
$(".old").replaceWith($(".new"));
});
});
Answered By - Rudresh Panchal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.