I am working on a product shortlist feature. I have grid view for each product & I have provided a shortlist link for each of the grid cell. Now I want to hide all these links in each cell initially on page load & show a specific link within the cell when a user hovers on that particular cell and again hide that link when user moves the cursor out of the cell. This cell is nothing but a div.
The shortlist link is within a div having the class product-grid-item.This div is nothing but a grid cell on which I want to hover. Actually using toggle function I am able to show/hide the shortlist link, but my code is generating multiple grid cells i,e div so when i hover on a particular grid the link is getting displayed but it also display all the links within the other grids as well.I just only want to show the particular link within the grid on which user hovers not all the links within the other grid cells.
How can I achieve this using jQuery? This is my code:
<?php foreach ($products as $index=>$product):?>
<div class="product-grid-item grid-cell">
<div class="shortlist" style="display: none; float:right;">
<?php
echo CHtml::ajaxLink('<i class="fa fa-star"></i> Shortlist',Yii::app()->createUrl('productshortlist/shortlistproduct'),
array('data'=>array('productId' => $product->product_id),
'dataType'=>'text',
'type'=>'get',
'success'=>'function(result)
{
if(result == "Success")
{
alert("Product Added to the shortlist successfully.");
$("#shortlistedProduct").prop("disabled",true);
}
else
$("#shortlist_product_error").html(result);
}'),
array('id'=>'shortlistedProduct'.$index)
);
?>
</div>
</div>
<?php endforeach;?>
Output:
<a id="shortlistedProduct0" href="#"><i class="fa fa-star"></i> Shortlist</a>
<a id="shortlistedProduct1" href="#"><i class="fa fa-star"></i> Shortlist</a>
Suggested but not working script:
<script>
$(".product-grid-item").hover(function()
{
$(".shortlist").toggle();
}
);
</script>