Search code examples
javascriptjqueryhtmlevent-bubbling

Child not triggering parent function


I have a menu and set an even listener to the document and if the a tag is clicked a function is executed, but for some reason when the elements inside the anchor tag are clicked the function is not triggered. Maybe I'm mistaken, but isn't default bubbling behavior for a parent function to execute when a child element is clicked?

document.addEventListener('click', function(e){
    if(e.target.tagName=="A"){
     alert('BUTTON CLICKED');
    }
  })
  
body {
    padding:0;
    margin:0;
    background-color: #252526;
}
* {
    box-sizing: content-box;
    font-family: 'arial', 'Arial';
}
a {
    text-decoration: none;
    color:white;
}
header {
    position: fixed;
    z-index:10;
    background-color:#2e2f30; 
    bottom: 0;
    left:0;
    right:0;
    padding: 10px 0px;
    display: block;
}
nav {
    display: block;
}
ul.navbar, li.nav-item {
    list-style-type: none;
    padding:0;
    margin:0;
}
.navbar {
    width:100%;
    position: relative;
    display: flex;
}
.nav-item {
    width:25%; 
    text-align: center;
    color:white;
}
.nav-item p {
    margin:0;
    padding:0;
    font-size: .9em;
    font-weight: 100;
}
<link href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
        <nav>
            <ul class="navbar">
                <li class="nav-item">
                    <a href="#home">
                    <i class="fas fa-home"></i>
                    <p>Home</p>
                    </a>
                </li>
            </ul>
        </nav>
    </header>
    <div class="page active-page" id="home">

    </div>

Note that if you click on the anchor tag but not on it's children elements the function will execute. How can I make the anchors tag's children execute the function as well? Any help appreciated.


Solution

  • You can use one more condition in OR operator as e.target.parentNode.tagName == "A" to check that the anchor element is the parent of the clicked element and it will work for you:

    document.addEventListener('click', function(e){
        if(e.target.tagName=="A" || e.target.parentNode.tagName == "A"){
         alert('BUTTON CLICKED');
        }
      })
    body {
        padding:0;
        margin:0;
        background-color: #252526;
    }
    * {
        box-sizing: content-box;
        font-family: 'arial', 'Arial';
    }
    a {
        text-decoration: none;
        color:white;
    }
    header {
        position: fixed;
        z-index:10;
        background-color:#2e2f30; 
        bottom: 0;
        left:0;
        right:0;
        padding: 10px 0px;
        display: block;
    }
    nav {
        display: block;
    }
    ul.navbar, li.nav-item {
        list-style-type: none;
        padding:0;
        margin:0;
    }
    .navbar {
        width:100%;
        position: relative;
        display: flex;
    }
    .nav-item {
        width:25%; 
        text-align: center;
        color:white;
    }
    .nav-item p {
        margin:0;
        padding:0;
        font-size: .9em;
        font-weight: 100;
    }
    <link href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <header>
            <nav>
                <ul class="navbar">
                    <li class="nav-item">
                        <a href="#home">
                          <i class="fas fa-home"></i>
                          <p>Home</p>
                        </a>
                    </li>
                </ul>
            </nav>
        </header>
        <div class="page active-page" id="home">
    
        </div>