Reputation: 57
How can I combine a text string with a variable for use as an array index?
For example, on this line from the code below:
$('.flip[id="item-3"]').fadeOut( 1500);
how do I do something like: $('.flip[id="item-" + id]').fadeOut( 1500);
To make this div fade away: < div id="item-3" class="flip">?
$( ".delete" ).click(function() {
var $this = $(this), id = $this.data('id');
$.ajax({
method: "post",
url: "charts_manage.php",
data: { id: id, do: 'delete' },
success: function( data ) {
$('.flip[id="item-3"]').fadeOut( 1500);
}
});
});
Upvotes: 2
Views: 2499
Reputation: 51
Javascript allows you to concatenate strings with ints by simply adding them. You can create the string by concatenating the first part of the string, the id int, and the end of the string:
'.flip[id="item-' + id + '"]'
if id=3, this gives you ".flip[id="item-3"]"
.
Upvotes: 0
Reputation: 193291
Insert variable in a middle like this:
$('.flip[id="item-' + id + '"]').fadeOut(1500);
Also it may be more comprehensive to use dedicated id selector:
$('#item-' + id).fadeOut(1500);
Upvotes: 3