javascriptjqueryhtmlselectorjquery-on

How to use NOT selector for selector of .on() using jquery?


I'm currently using this to select fields:

$('body').on('change', '#mainFrom input', function(){

How do I apply a not selector for class listName ?

I've tried :

$('body').not('.listName').on('change', '#mainForm input', function(){

Which didn't work.

How do I do this ?

Thanks


Solution

  • I'm going to guess that the listName class will be on the input. If so:

    $('body').on('change', '#mainForm input:not(.listName)', function(){
    // ------------------------------------^^^^^^^^^^^^^^^
    

    Or if we guess that it's on #mainForm:

    $('body').on('change', '#mainForm:not(.listName) input', function(){
    // ------------------------------^^^^^^^^^^^^^^^
    

    Or if we guess that it's on body, we have to change more:

    $( document ).on('change', 'body:not(.listName) #mainForm input', function(){
    // ^^^^^^^^-----------------^^^^^^^^^^^^^^^^^^^