div calling two different functions

Denis Ximenes

I have two different divs that they call different functions when you click on them. The div that has the class="msg1" calls a function called message1() that brings one alert saying('this is message 1'). This is working well

My problem is that I have another div called class="msg2" that it is inside of the the div class="msg1" and when I click on this div class="msg2" it calls the function message2() AND ALSO the function message1(), which I don't want.

When I click on the div class="msg2" I JUST want to call the function message2(). How can I just execute the function message2() when I click on the div class=msg2?

const message1 = () => {
  alert('this is message 1')
}

const message2 = () => {
  alert('this is message 2')
}
.msg1 {
  width: 600px;
  height: 600px;
  background-color: darkgoldenrod;
  display: flex;
  align-items: center;
  justify-content: center;
}

.msg2 {
  width: 300px;
  height: 300px;
  background-color: dimgrey;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="msg1" onclick="message1()">
  <div class="msg2" onclick="message2()">
  </div>
</div>

brk

You can stop the propagation of the event from the child div using event.stopPropagation()

const message1 = (event) => {
  alert('this is message 1')
}

const message2 = (event) => {
  alert('this is message 2');
  event.stopPropagation();
}
.msg1 {
  width: 600px;
  height: 600px;
  background-color: darkgoldenrod;
  display: flex;
  align-items: center;
  justify-content: center;
}

.msg2 {
  width: 300px;
  height: 300px;
  background-color: dimgrey;
  display: flex;
  align-items: center;
  justify-content: center;
}
<div class="msg1" onclick="message1(event)">
  <div class="msg2" onclick="message2(event)">
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling two different functions from onKeyDown event

From Dev

calling different functions from inside and outside of div

From Dev

Calling two functions with the same name from different sources()

From Dev

calling two different functions using ng-click

From Dev

Calling different JavaScript functions

From Dev

Calling a delegate on two functions

From Dev

Calling functions with different number of parameters

From Dev

Calling class functions of different forms

From Dev

Calling functions with different arg count

From Dev

Calling different functions inside a class

From Dev

onclick calling two functions simultaneously

From Dev

ReactJS calling two functions with onClick

From Dev

calling different functions from different divs

From Dev

Calling Different Functions in Python Based on Values in a List

From Dev

Calling functions with different numbers of arguments in Rust macros

From Dev

Different behaviour with calling functions inside and outside objects

From Dev

MATLAB: OOP Calling Functions from Different Class

From Dev

Calling the same function with different functions called inside

From Dev

Python design for calling functions with different signatures

From Dev

Call variables in two different functions

From Dev

Why is the signature different for these two functions?

From Dev

Unify two different "resize" functions

From Dev

why results of these two functions are different?

From Dev

Two different outputs from functions

From Dev

Calling two ajax functions on form submission

From Javascript

React hooks - calling two functions in one hook

From Dev

Passing struct pointer to two functions and then calling malloc

From Dev

Why is 'this' different things in two different functions?

From Dev

One div two functions in jquery

Related Related

  1. 1

    Calling two different functions from onKeyDown event

  2. 2

    calling different functions from inside and outside of div

  3. 3

    Calling two functions with the same name from different sources()

  4. 4

    calling two different functions using ng-click

  5. 5

    Calling different JavaScript functions

  6. 6

    Calling a delegate on two functions

  7. 7

    Calling functions with different number of parameters

  8. 8

    Calling class functions of different forms

  9. 9

    Calling functions with different arg count

  10. 10

    Calling different functions inside a class

  11. 11

    onclick calling two functions simultaneously

  12. 12

    ReactJS calling two functions with onClick

  13. 13

    calling different functions from different divs

  14. 14

    Calling Different Functions in Python Based on Values in a List

  15. 15

    Calling functions with different numbers of arguments in Rust macros

  16. 16

    Different behaviour with calling functions inside and outside objects

  17. 17

    MATLAB: OOP Calling Functions from Different Class

  18. 18

    Calling the same function with different functions called inside

  19. 19

    Python design for calling functions with different signatures

  20. 20

    Call variables in two different functions

  21. 21

    Why is the signature different for these two functions?

  22. 22

    Unify two different "resize" functions

  23. 23

    why results of these two functions are different?

  24. 24

    Two different outputs from functions

  25. 25

    Calling two ajax functions on form submission

  26. 26

    React hooks - calling two functions in one hook

  27. 27

    Passing struct pointer to two functions and then calling malloc

  28. 28

    Why is 'this' different things in two different functions?

  29. 29

    One div two functions in jquery

HotTag

Archive