shibbir ahmed shibbir ahmed - 2 years ago 89
jQuery Question

How to hide individual class in php while loop using jQuery?

I have following

loop. It's showing for e.g. 4 Images.

while( $get_images_result = mysqli_fetch_array($get_images) ) {

$image_id = (int) $get_images_result['p_image_id'];
$p_large_image = htmlspecialchars( $get_images_result['p_large_image'] );
$p_small_image = htmlspecialchars( $get_images_result['p_small_image'] );
echo '<div class="col-md-3 existingImg">';
echo '<div class="my_menu_image">';
echo "<img src='".IMG_DIR."menu_images/$p_small_image' class='img-responsive' width='160' height='112' />";
echo "<input type='hidden' id='p1' value='$p_large_image'>";
echo "<input type='hidden' id='p2' value='$mid'>";
echo "<input type='hidden' id='p3' value='$u_id'>";
echo '<a class="icon-jfi-trash jFiler-item-trash-action menu_image_delete"></a>';
echo '</div>';
echo '</div>';

And you can see that there is a
class in this line
echo '<a class="icon-jfi-trash jFiler-item-trash-action menu_image_delete"></a>';

class is used for delete image. So once I delete each image it's successfully delete the image from Database and then I want to hide those selected image which I deleted. So that I am using following jQuery code but can't get it working :(

JQuery Code :

$('.menu_image_delete').click(function() {
if ( confirm( 'Are you sure you want to delete this?') ) {
var p1 = $('#p1').val();
var p2 = $('#p2').val();
var p3 = $('#p3').val();
var param = 'delete_upload_image?p1='+p1+'&p2='+p2+'&p3='+p3;

$('.existingImg').each(function() {
$('.existingImg', $(this) ).hide();


Answer Source

Use this in event handler which is the actual element event occurred on. Then traverse up to existingImg using closest()


$('.existingImg').each(function() {
     $('.existingImg', $(this) ).hide();



Note however that you should really only do this in success callback of $.get otherwise if the ajax request fails user will still see element get hidden and assume all is well.

$.get(param, function(resp){
    // validate your response here and then hide image
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download