on click on a different selector than the one you want

1

I must execute a function in JQuery when I click on a class other than "hello", that is, when I click on any element that does not have the "hello" class, I execute a function, something like:

$(document).on("click touchend",!".hola", function(e){ ......});

How could I do it?

EDIT I leave the code to see what error I have, the classes I want to click are inside tds:

<script>
$(document).ready(Principal);
    function Principal(){
    $(document).on("click", function(){
        if(!$(this).hasClass('hola')){
            console.log("clickeaste afuera"+$(this).attr("class"));
        }else{console.log("clickeaste adentro"+$(this).attr("class"))}

    });
    }
</script>

<body>
    <table>
    <tr><td class="sdha09001200hea">09:00 - 12:00</td><td>09:00 - 12:00</td></tr>
    <tr><td class="ashi10001330hod">10:00 - 13:30</td><td>10:00 - 13:00</td></tr>
    <tr><td class="hola">hola</td><td>holita</td></tr>
    </table>
</body>
    
asked by Roberto Sepúlveda Bravo 25.10.2016 в 04:51
source

2 answers

3

You can use the hasClass() method, an example:

$(event).on('click touchend', function() {
    if (!$(this).hasClass('hola')) {
        // El elemento clickeado no tiene la clase hola
    }
    return false;
});

$(document).ready(function(){
  $('button').on('click', function (){
    if(!$(this).hasClass('hola')){
      alert('Este botón no tiene la clase hola')
    }
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="hola" >HOLA</button>
<button type="button" class="adios" >ADIOS</button>
    
answered by 25.10.2016 / 05:09
source
2

You could use the :not() selector in jQuery to indicate that events should be applied to all elements except those with class .hola . Something like this:

$("body").on("click touchend", ":not(.hola)", function(e){
  console.log("not hola");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="num1">1111</div>
<div class="hola">Hola</div>
<div class="num2">2222</div>
    
answered by 25.10.2016 в 05:32