1

I have a div and inside it there are some elements.

my parent div has a test() function by using onClick. in inside parent div there is a quickView() function by using onClick.

now, when I click on quickView() function, test() function fired .

<div onClick="test();" class="product-inner">
  <h5 class="product-title height-40-px">


    title

    </p>
  </h5>
<hr class="mt8 mb8">
<div class="product-meta">
  <ul class="product-price-list">

    <li><span class="product-save dir-to-right"><strong>7500</strong></span>
    </li>
    <li><span class="product-old-price dir-to-right">120,000</span>
    </li>
    <li><span class="product-price">%95</span>
    </li>
  </ul>
  <hr class="mt0 mb0 mr6 hr-blue">
  <div class="col-md-12 mt10">

    <span class="col-md-6">
      ۹&nbsp;<i class="fa fa-users"></i>
    </span>

    <span class="col-md-6">

      <p class="font-size-12 display-inline">


        zone
        &nbsp;<i class="fa fa-map-marker"></i>


      </p>

    </span>

  </div>

  <ul class="product-actions-list mt35">

    <li><a data-original-title="add cart" class="btn btn-cart btn-sm popup-text" href="#product-quick-view-add-to-cart" onclick="quickView(18730);" data-effect="mfp-move-from-top" data-toggle="tooltip" data-placement="top" title="">cart&nbsp;<i class="fa fa-shopping-cart"></i></a>
    </li><li><a href="" class="btn btn-buy btn-sm">details&nbsp;<i class="fa fa-eye"></i></a>
    </li>
  </ul>
</div>
</div>
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

3 Answers3

2

Use event.stopPropagation() to prevent the click event from propagating to the parent. This is how you use it.

P.S. IE supports e.cancelBubble instead of e.stopPropagation()

function quickView(value, e) {
 if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
  console.log(value);
}

function test() {
  console.log('test Fired');
}
<div onClick="test();" class="product-inner">
  <h5 class="product-title height-40-px">


    title

    </p>
  </h5>
<hr class="mt8 mb8">
<div class="product-meta">
  <ul class="product-price-list">

    <li><span class="product-save dir-to-right"><strong>7500</strong></span>
    </li>
    <li><span class="product-old-price dir-to-right">120,000</span>
    </li>
    <li><span class="product-price">%95</span>
    </li>
  </ul>
  <hr class="mt0 mb0 mr6 hr-blue">
  <div class="col-md-12 mt10">

    <span class="col-md-6">
      ۹&nbsp;<i class="fa fa-users"></i>
    </span>

    <span class="col-md-6">

      <p class="font-size-12 display-inline">


        zone
        &nbsp;<i class="fa fa-map-marker"></i>


      </p>

    </span>

  </div>

  <ul class="product-actions-list mt35">

    <li><a data-original-title="add cart" class="btn btn-cart btn-sm popup-text" href="#product-quick-view-add-to-cart" onclick="quickView(18730, event); " data-effect="mfp-move-from-top" data-toggle="tooltip" data-placement="top" title="">cart&nbsp;<i class="fa fa-shopping-cart"></i></a>
    </li><li><a href="" class="btn btn-buy btn-sm">details&nbsp;<i class="fa fa-eye"></i></a>
    </li>
  </ul>
</div>
</div>
Mojo Chinto
  • 41
  • 1
  • 8
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
1

Click events are bubbling up from the inner element clicked up to the document root going through all the parents of the clicked element. in order to stop it from propagating up you should use - event.stopPropagation();

naortor
  • 2,019
  • 12
  • 26
1

That's how event bubbling works. First the click event is triggered on the element that actually was clicked (a.btn.btn-cart in your case) and then it goes upwards and reaches the parent div with test(). You can prevent further event propagation by passing an event to your quickView() function and calling event.stopPropagation(). Then, your HTML should look like this:

<a class="btn btn-cart btn-sm popup-text" onclick="quickView(event, 18730);">Cart</a>

And your JavaScript function:

function quickView(event, number) {
    event.stopPropagation();
    console.log(number) // Do some stuff with the passed ID.
}
EternalLight
  • 1,323
  • 1
  • 11
  • 19