Hi all i am working on jquery i need to avoid adding multiple images i had written the code here but
always i am getting index value of image is -1 and always if condition fires i dong some thing wrong
could u plz help me to do that thanks in advance
Here is the code for the first div
<div><span class="span1">
<img width="30" hieght="30" name="productimage" src="http://panther:805/Computers.jpg"></span>
<span class="span1">Black_xxl_Slim</span>
<span class="span1">1</span>
<a class="pull-right" href="#"><i onclick="Add(this)" class="icon-plus"></i></a>
</div>
Here is the code for the second div:
<div class="span6" id="separat">
<ul class="thumbnails pre-scrollable" id="productbundles">
</ul>
</div>
here my code follows
function Add(obj) {
var addedImagesArray = [];
var img = $(obj).closest('div').find('img').first();
var image_src = $(img).attr('src');
var newobj = $('<li class="span2" id="bunle' + id++ + '"><a href="javascript: void(0)"><img hieght=30 width=30 src="' + image_src + '" /></a><h5>' + "Name:" + name + '</h5><span id="pric' + id + '"><b>' + "Price:" + price + '</b></span>');
if (addedImagesArray.indexOf(image_src)) {
alert("Image Already Added");//src present in the array so do nothing or show an alert..
} else {
$('#productbundles').append(newobj); //append the image
addedImagesArray.push(image_src); ///add src to array
}
}
on above iam having two divs i am adding images from one div to another
but problem is i am getting -1 index value always plz help me to do this thanks in advance
You need to compare returned value of indexOf() with
-1, also declare addedImagesArray in global scope out side the function.Change
To
Declare addedImagesArray in global scope