Reputation: 181
If I use the following code and only list one element then it works. When I want to remove both elements, it doesn't. This is apart of my AJAX Success call:
jQuery('#t_'+$cid, '#'+$cid).remove();
Can I not do this in this manner?
Upvotes: 2
Views: 201
Reputation: 33933
The selector has to be ONE string... So the coma separator for multiple selectors has to be inside the string.
This snippet works:
let $cid = 1
jQuery('#t_'+$cid + ', #'+$cid).remove();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="t_1">t_1</div>
<span id="1">1</span>
Since that is starting to be messy and hard to nails down mistakes... When it comes to use variables in the selector(s) string, I suggest you to use some templating literals. So this would be easier to read:
jQuery(`#t_${$cid}, #${$cid}`).remove();
Upvotes: 2