Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
How to stop propagating event from parent div in child select

There's a div element that contains 1 or more child elements of type select. When the div (my_title) is clicked, sort() has to run (OK). If a value is selected from the select (my_select), sort() is executed as well but it shouldn't. I tried to add a click handler and call stopPropagation() but sort() is still called. In fact, sort() is called first and only then stopPropagation().

<div id="my_title" onclick="sort(&quot;sf&quot;, &quot;2&quot;, event)" style="top: 0px; left: 1px; width: 300px; height: 50px; text-align: center; position: absolute;">
   Column 2
   <select id="my_select" style="top: 1px; left: 0px; width: 297px; position: relative;" >
      <option value="1">1</option>
      <option value="2">2</option>
   </select>
</div>

JavaScript:

function sort() {
    console.log('sort');
}

$('#my_title').on('click', '> select', function(event) {
    console.log('stopPropagation');
    event.stopPropagation();
    // event.cancelBubble = true;
    // event.stopImmediatePropagation();
});

Fiddle: https://jsfiddle.net/Smolo/34nh6Lwb/

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Seems that that behavior happens when you use onclick="..."

Try using this instead:

$('#my_title').on('click', function(event) {
  sort()
})
about 4 years ago · Juan Pablo Isaza Relatório

0

You cannot stop event propagation between the target element and the delegated parent when using a delegated event handler. This is because the event needs to propagate up the DOM from the element that raises the event to the one which the handler is bound to.

A better solution in this case is to determine which element raised the click event handler and then only call sort() if it was the #my_title element.

Also note that your JS can be improved. Firstly, you should not bind JS event handlers or add styling in to the HTML. They should be placed in external JS and CSS files, respectively.

In addition, event handlers should be bound unobtrusively (as you do for the select element) not using outdated onX attributes which are bad practice.

Finally, use the change event on the select to listen for updates, not the click event.

With all those points addressed, your code should look like this:

jQuery($ => {
  $('#my_title').on('click', e => {
    let $el = $(e.target);
    if ($el.is('#my_title'))
      sort($el.data('sort-key'), $el.data('sort-value'));
  });

  $('#my_title').on('change', '> select', function(event) {
    // change handler logic here...
    console.log('change');
  });
});

let sort = (key, value) => {
  // your sort logic here...
  console.log(key, value);
}
#my_title {
  top: 0px;
  left: 1px;
  width: 300px;
  height: 50px;
  text-align: center;
  position: absolute;
}

#my_select {
  top: 1px;
  left: 0px;
  width: 297px;
  position: relative;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="my_title" data-sort-key="sf" data-sort-value="2">
  Column 2
  <select id="my_select">
    <option value="1">1</option>
    <option value="2">2</option>
  </select>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Change your sort to something like this:

function sort(a, b, e) {
        if (e.target.id === 'my_title') { // or use more generic comparison
            console.log('sort');
    }
}

It will only run for elements that much your comparison (eg your Label/Title

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda