Trouble with jQuery on click function with dynamic objects
I have some dynamically generated cards on my app that are generated by jinja. The cards contain a few forms - 2 of which I'd like to toggle. I wrote a simple JS function to accomplish this. The problem was that the function only worked on the first card.
I re-implemented in jQuery, but I am still getting the same result - the function only works on the first card. Any advice on what I might be doing wrong?
The jquery function (this is in the scripts block at the top of index.html) - It uses bootstrap-flask:
<script>
$(document).ready(function(){
$("#toggle").on("click", function() {
const $moveCashForm = $(this).closest('#category').find('#move-cash');
const $addCashForm = $(this).closest('#category').find('#add-cash');
if ($moveCashForm.hasClass('hidden')) {
$moveCashForm.removeClass('hidden');
$addCashForm.addClass('hidden');
} else {
$addCashForm.removeClass('hidden');
$moveCashForm.addClass('hidden');
}
console.log($(this).closest('#category').find('#move-cash'));
});
});
</script>
The portion of the jinja template that dynamically generates the cards:
{% for category in categories %}
<div class="col-md-4 col-sm-6">
<div class="card border-secondary mb-3 text-center" id="category">
<img class="card-img-top" src="{{ url_for('static', filename = 'envelope.jpg') }}">
<div class="card-body">
<h4 class="card-title" name="{{ category.id }}">{{ category.name }}</h4>
<button type="button" class="btn btn-default" id="toggle">{{ format(category.amount) }}</button>
<div id="add-cash">
<form action="" method="post" class="form" role="form">
{{ add_to_category_form.csrf_token() }}
{{ add_to_category_form.category_id(value=category.category_id) }}
{{ wtf.render_field(add_to_category_form.amount) }}
<div class="add-cash-button">
{{ add_to_category_form.submit(class_="btn btn-primary") }}
</div>
</form>
<form action="" method="post" class="form" role="form">
<div class="delete-button">
{{ delete_category_form.csrf_token() }}
{{ delete_category_form.category_id(value=category.category_id) }}
{{ delete_category_form.delete(class_="btn btn-danger") }}
</div>
</form>
</div>
<form action="" method="post" class="form hidden" role="form" id="move-cash">
{{ move_to_category_form.csrf_token() }}
{{ move_to_category_form.from_category_id(value=category.category_id) }}
{{ wtf.render_form(move_to_category_form) }}
</form>
</div>
</div>
</div>
{% endfor %}
I hope that was clear... Any help is appreciated. I've been stumped for a couple hours now. Thanks!
Posted Oct 17, 2020, 1:56 AM UTC
13
ID's on elements need to be unique, this creates a element with ID of 'toggle' several times. In your browser's dev tools you'll probably see an error that says you have a non-unique ID for elements. The solution here is to say class="toggle", then change your jQuery selector to $('.toggle'). You might also have to use jQuery's .each function to loop through each .toggle element and add the onclick listener to each.
Great insight, thanks. I think I initially used class names, but i could be wrong. I tried so many combinations. I’ll definitely look into the each function. Though
Good to go now, changed all selectors to class names...
Hate when it's something so simple, but it always is.
Thanks for taking the time!