0

I have few namespaces and I want to reinitialize function inside namespaces on document change in order to be reinitialized every time when the document is modified (*modified = adding/removing new sections on existing dom ). I have tried this but not working so far:

 ;namespaceName= {
        namespaceFunction1: function() {
            $( selector ).on('click', function() {
                //my first function run here
            })
        },
        // ************second function in namespace***************/

        namespaceFunction2: function() {
            $(secondSelector).on('click', function() {
            //my second function run here
            })
        }
    }

    $(document).on('change', namespaceName.namespaceFunction1() );
    $(document).on('change', namespaceName.namespaceFunction2() );

Pls help, ty.

RulerNature
  • 693
  • 4
  • 13
  • 40
  • 2
    There is no document change event (and you would need to remove the empty parenthesis from the functions anyway)... http://stackoverflow.com/questions/12495128/jquery-does-not-execute-on-document-change – Reinstate Monica Cellio May 09 '14 at 13:33
  • `document` does not have a `change` event. – kei May 09 '14 at 13:33
  • @Archer,@kei so according t your knowledge this function will never work, right? $(document).on('change', 'input:radio', function(){ var el = $(this).closest('.input-group').find('input:text'); $('input:text').prop('disabled', function(){ return !el.is(this); }) }) – RulerNature May 09 '14 at 13:39
  • 1
    That's entirely different. That's looking for a change event on all radios *within* the document. That's not what your code above is doing. It's trying to capture a change event on the document object itself, which does not exist. – Reinstate Monica Cellio May 09 '14 at 13:41
  • If you explain what it is you're trying to achieve then maybe we can help. What do you mean by "document change"? – Reinstate Monica Cellio May 09 '14 at 13:47
  • @archer yes but that function if running up no matter if the dom was modified, maybe I have not explained very well my question but I need something that reinitialize methods from namespace when dom is modified. document change = dom modified in my opinion. – RulerNature May 09 '14 at 13:48
  • I'll post an answer for you then. – Reinstate Monica Cellio May 09 '14 at 13:52
  • @Archer yes something like that ty :) – RulerNature May 09 '14 at 13:59

2 Answers2

1

sounds like you need to listen for the DOMSubtreeModified event like this:

$('body').bind('DOMSubtreeModified', function(){
      //your code here
});
1

Try this...

$(document).on("DOMSubtreeModified", function () {
    namespaceName.namespaceFunction1();
    namespaceName.namespaceFunction2();
});

It fires your 2 functions on the DOMSubtreeModified event, which is basically what you were looking for - when the DOM changes.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67