removeChild works only out of the img

I want to remove overlay when i was clicking and the image but it didn’t. The overlay dissapearing when i clicked on div (overlay) but not when I clicked on photo.The console didn’t show me errors.

(function (){

    window.onclick=function (e){

        overlay.addEventListener('click',function (){

     window.onscroll=function (){


Could you show us the markup this is being applied to?

<p><img class="alignright" src="images/<?php echo $post_image; ?>" width="200" height="150" alt="" /></p>

and all the document

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<title>NewsTheme - Demo Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<!--[if lte IE 6]><link rel="stylesheet" type="text/css" href="ie.css" /><![endif]-->
<script src="js/wordpress.js"></script>
<!-- BEGIN navigation -->
<?php include 'main_includes/navigation.php';   ?>
 <?php include 'main_includes/connect_db.php';   ?>
<!-- END navigation -->

<!-- BEGIN wrapper -->
<div class="wrapper">
  <!-- BEGIN header -->
  <div id="header"><h2 id="epikefalida">ΔΗΜΟΣ ΑΡΓΟΥΣ ΟΡΕΣΤΙΚΟΥ  Γρ.Γεωπόνου</h2> 
      <div class="photo"><img src="images/teliko.jpg" alt="" height="150px" width="430" style="margin-top: 0px;margin-left: 0px;"/></div>
  <!-- END header -->
  <!-- BEGIN body -->
  <div id="body">
    <!-- BEGIN content -->
    <div id="content">
      <!-- begin post -->
      <div class="recent post">
            $query="SELECT * FROM posts WHERE post_id=$post_id ";
            $select_query= mysqli_query($connect, $query);
            $num_rows=  mysqli_num_rows($select_query);
            $row=  mysqli_fetch_assoc($select_query);
        <h2><?php echo $post_title; ?></h2>
        <p><img class="alignright" src="images/<?php echo $post_image; ?>" width="200" height="150" alt="" /></p>
        <p><?php echo $post_keimeno; ?></p>
        <p><?php echo "$post_publisher"."&nbsp&nbsp&nbsp&nbsp&nbsp"."$post_date"; ?></p>
      <!-- end post -->
    <!-- END content -->
    <!-- BEGIN sidebar -->
    <?php include 'main_includes/sidebar.php';  ?>
    <!-- END sidebar -->
  <!-- END body -->
  <div class="break"></div>
<!-- END wrapper -->

<!-- BEGIN footer -->
<?php include 'main_includes/footer.php';  ?> 
<!-- END footer -->


It would be more useful to have the HTML that’s being output, rather than the PHP generating it, that way I can quickly set up a demo to look at what’s going on.

I knocked up a quick Codepen with some minimal markup in it just to see what’s happening. As described, when clicking on the image in the markup, the overlay appears. The overlay will clear when the overlay itself is clicked, it doesn’t though when the image set in the overlay is clicked.

I’ve not got an immediate solution for you, but I did add a line into the event listener to log out where on the overlay it will respond to a click event. Seemingly, it responds when the image is clicked, which makes it a bit of a puzzle as to why the function attached to the listener isn’t run.

    overlay.addEventListener('click', function() {
      console.log('Logged from ' + overlay);

Here’s the Codepen

1 Like

Thanks a lot my friend but unfortunately not working.I scratch my head the last 4 hours to understand why this is happen.

Likewise. Currently, my JS is in its formative stage, so I’m hoping a more experienced head will contribute something at some point.

1 Like

The overlay is being removed when you click on the image, but it’s immediately being re-added, which is why it appears that nothing is happening.

Here’s what’s happening:

  • The first click creates and displays the image overlay
  • A click handler is added to the overlay
  • Clicking the overlaid image fires the handler to remove it
  • The click handler on the window object then fires in response to the same click, and re-adds the overlay

One way to prevent this is to avoid adding the first click handler to the window object, and instead add it to the image. Alternatively, you can call stopPropagation() on the event, to stop it from travelling up from the overlay to the window (as events ‘bubble’ up the DOM from the target):

overlay.addEventListener('click', function(e) {

WORKING!!Thanks a lot lot lot my friend.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.