Two Clicks Javascript in one place, can one cancel the other?

Asked

Viewed 34 times

-1

I have a little problem, and I would like, please, to have your help. In IMAGE 1: I have a click ($('. open-box'). click(Function (Openbox) {) on line 170, but within this line I have another click ($('. Arq-Folder'). click(Function (arqfolder) {) on line 173.

If I click on any area of the line of IMAGE 2 where I indicate with a red arrow the click of the class "open-box" runs, this is OK, the "little problem" is that when I click on the blue icon (Folder) to run the click of the class "Arq-Folder", the click of the class "open-box" also runs, but this could not happen! It seems impossible, because the class "Arq-Folder" is of a div that is inside the div with the class "open-box". My question is: If it is possible to click class "Arq-Folder", type cancel the click of class "open-box"? I’m just trying and trying... Aff. Imagem vale mais que mil palavras (rs)

1 answer

2


You can call the method stopPropagation at the event.

Imagine that when you click on some element of your page, an event is created. This click event is generated in the element you clicked on, and then is raised to its parent element, and then raised to the parent element of the parent, and so on until you get to the root of your GIFT.

You added a eventListener in the elements arq-folder and open-Box, this means that there is a component listening to events in these elements, when a click event passes through these elements, the function you defined in these listeners is invoked.

If you don’t want the event captured on arq-folder be captured in the open-Box, just call the method stopPropagation in that event, and it will no longer be elevated to parent elements. As the event will not be elevated until the open-Box, the function of this element will not be activated. Example (click on the table row):

$('.open-Box').click(e => {
  console.clear()
  console.log('Ativou o evento do "open-Box"')

})

$('.arq-folder').click(e => {
  e.stopPropagation()
  console.clear()
  console.log('Ativou somente o evento do "arq-folder"')
})
table {
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  padding: 8px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <th>S</th>
    <th>A</th>
    <th>Cliente</th>
    <th>Descrição do produto</th>
  </tr>
  <tr class="open-Box">
    <td></td>
    <td class="arq-folder"></td>
    <td>João Batista de Carvalho Azevedo</td>
    <td></td>
  </tr>
</table>

  • SEN-SA-CI-O-NAL man! too... thank you very much... but there was a conflict and I had to remove the script from the page (include from php) and take it to a parent page and did like this: <div class='col1 emCenter' onclick='active("<? php echo $eidneg; ? >");' data='<? php echo $eidneg; ? >' style='width:30px;'> and in javascript: Function active(value) {... Aff Aff cof cof cof... stopPropagation now does not want to funfar!!! ai (Obs: I will remove the date='<? php echo $eidneg; ? >', because I am still testing many things...)

  • Dude... too... check out what would solve the case: onclick='Event.stopPropagation(); active("<?php echo $eidneg; ?>");' What did Dr. Mystery (alias user140828)? Do you approve of this? Do you think it’s cool? Solved! What do you think? Thank you brother!

  • @Infonet, I have no idea what’s going on in this code that you posted in the comments. The code is incomplete, and I don’t know its context. It would be better if you edit your question, or even make a new one, since apparently now the problem is another.

  • Dude! It’s hot! You already answered the question... blz... grade 11 procê! Leave it like this... I just have to thank you for the kindness you have granted in promptly answering my question. I am so grateful! God enlighten you.

  • After reading a lot about the solution: onclick='Event.stopPropagation(); active("<?php echo $eidneg; ?>");' _ I realized that it is functional and used without restriction, so I solved the problem, since I am using "onclick", but if I had not changed my code, I would use it e. stopPropagation() for jquery. My sincere thanks to dr. mystery (rs) @user140828, for the kindness in helping me. May God enlighten you for this!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.