Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove image and its container if image is broken

I am just trying to remove image and its title in case if image fails to load or broken using html or JavaScript.what is the best and easiest way to do that?

<?php


   include("../includes/db.php");   // ../ means go to parent directory
  $query='SELECT * FROM `table` WHERE tag NOT IN("news") ORDER BY `id` DESC LIMIT 0,5';
  $run=mysqli_query($conn,$query);
  while($row=mysqli_fetch_array($run)){
         $img=$row['src'];
         $title=$row['title'];
   ?>

       <table class='mobileTable'>
          <tr> <td><img src='<?php echo $img ?>' onerror='imgError(this)' /></td></tr>
          <tr> <td  class='mobMidTitle'><?php echo $title ?></td></tr>//i want to remove this title as well
       </table>
   <?php } ?>

 <script>
 function imgError(image) {
    image.onerror = "";
    image.src = "http://localhost/domain/images/error.jpg";
    document.getElementById('tit').style.display='none';
    return true;
}</script>

code above will fetch 5 images from database and with javascript i can change image to error.jpg but i want to remove both image as well as its title only of failed image.

like image 712
Yugraaj Sandhu Avatar asked Dec 19 '22 08:12

Yugraaj Sandhu


2 Answers

You can accomplish this in one of two ways:

Either use the removeChild function (assuming you want to remove the elements and not hide them):

function imgError(image) {
   // Get image row
   var imageRow = image.parentNode.parentNode;
   // Get image label row
   var imageLabel = imageRow.nextElementSibling;
   // Get table element of that image row and remove imageLabel
   imageRow.parentNode.removeChild(imageLabel);
   // Get table element of that image row and remove img element
   imageRow.parentNode.removeChild(imageRow);
}

Or you can instead hide your elements using the .style.visibility property of the image and label elements:

 function imgError(image) {
    // Get image row
    var imageRow = image.parentNode.parentNode;
    // Get image label row
    var imageLabel = imageRow.nextElementSibling;
    // Hide imageLabel
    imageRow.style.visibility = 'hidden';
    // Hide imageRow
    imageLabel.style.visibility = 'hidden';
}
like image 85
kutacoder Avatar answered Dec 24 '22 01:12

kutacoder


well, there are multiple approaches to do this :

here, I'm assuming that you want to use Pure javescript -no jquery- and keeping the original DOM document hierarchy.

<table class='mobileTable'>
    <tr> <td><img src='path/to/broken/image.jpg' onerror='imgError(this)' /></td></tr>
    <tr> <td  class='mobMidTitle'>some title</td></tr>
</table>

<script>
function imgError(image) {
    // this will step up to get the parent of the image's parent
    var imgParent = image.parentElement.parentElement;
    // this will get the next sibling of the image grandpa
    var nextSib = imgParent.nextElementSibling;
    // reset it's html to empty
    nextSib.innerHTML = '';
    image.onerror = "";
    image.src = "http://localhost/domain/images/error.jpg";
    return true;
}
</script>

another easier approach is by adding new ID to your title TD and then remove it using javascript as follows :

/* here we've added id='mobMidTitle' */
<td  class='mobMidTitle' id='mobMidTitle' >some title</td>

<script>
function imgError(image) {
    image.onerror = "";
    image.src = "http://localhost/domain/images/error.jpg";
    document.getElementById('mobMidTitle').style.display = 'none';
    //                       ^^^^^^^^^^^
    return true;
}
</script>
like image 41
hassan Avatar answered Dec 24 '22 02:12

hassan