Equivalent document.querySelector inside Mutation Observer dynamic elements











up vote
2
down vote

favorite












I'm using Mutation Observer to listen to the new added elements and it's working fine for all elements but for Switchery jQuery Plugin it doesn't because Switchery get element by [document.querySelector]



This is my code...



MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

var observerjQueryPlugins = function (repeaterWrapper) {

_.each(repeaterWrapper, function (repeaterItem, index) {

var jq_nodes = $(repeaterItem.addedNodes);

jq_nodes.each(function () {

// Color Picker (Working Good)
$(this).find('.element-wrapper.element-wpcolor .color-picker').wpColorPicker();

// Switchery using document.querySelector and i need to know the
// equivalent way to do it inside this loop .. like that

// Of course this code is WRONG
sw_current = $(this).find('.switchery-element');

var switchery = new Switchery( sw_current, {
disabled: false,
size: '',
color: '#8ce196',
secondaryColor: '#ddd',
jackColor: '#fff',
jackSecondaryColor: '#fff'
});

});
});

}

new MutationObserver(observerjQueryPlugins).observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});


Thanks for your help.










share|improve this question


























    up vote
    2
    down vote

    favorite












    I'm using Mutation Observer to listen to the new added elements and it's working fine for all elements but for Switchery jQuery Plugin it doesn't because Switchery get element by [document.querySelector]



    This is my code...



    MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

    var observerjQueryPlugins = function (repeaterWrapper) {

    _.each(repeaterWrapper, function (repeaterItem, index) {

    var jq_nodes = $(repeaterItem.addedNodes);

    jq_nodes.each(function () {

    // Color Picker (Working Good)
    $(this).find('.element-wrapper.element-wpcolor .color-picker').wpColorPicker();

    // Switchery using document.querySelector and i need to know the
    // equivalent way to do it inside this loop .. like that

    // Of course this code is WRONG
    sw_current = $(this).find('.switchery-element');

    var switchery = new Switchery( sw_current, {
    disabled: false,
    size: '',
    color: '#8ce196',
    secondaryColor: '#ddd',
    jackColor: '#fff',
    jackSecondaryColor: '#fff'
    });

    });
    });

    }

    new MutationObserver(observerjQueryPlugins).observe(document.body, {
    childList: true,
    subtree: true,
    attributes: false,
    characterData: false
    });


    Thanks for your help.










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm using Mutation Observer to listen to the new added elements and it's working fine for all elements but for Switchery jQuery Plugin it doesn't because Switchery get element by [document.querySelector]



      This is my code...



      MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

      var observerjQueryPlugins = function (repeaterWrapper) {

      _.each(repeaterWrapper, function (repeaterItem, index) {

      var jq_nodes = $(repeaterItem.addedNodes);

      jq_nodes.each(function () {

      // Color Picker (Working Good)
      $(this).find('.element-wrapper.element-wpcolor .color-picker').wpColorPicker();

      // Switchery using document.querySelector and i need to know the
      // equivalent way to do it inside this loop .. like that

      // Of course this code is WRONG
      sw_current = $(this).find('.switchery-element');

      var switchery = new Switchery( sw_current, {
      disabled: false,
      size: '',
      color: '#8ce196',
      secondaryColor: '#ddd',
      jackColor: '#fff',
      jackSecondaryColor: '#fff'
      });

      });
      });

      }

      new MutationObserver(observerjQueryPlugins).observe(document.body, {
      childList: true,
      subtree: true,
      attributes: false,
      characterData: false
      });


      Thanks for your help.










      share|improve this question













      I'm using Mutation Observer to listen to the new added elements and it's working fine for all elements but for Switchery jQuery Plugin it doesn't because Switchery get element by [document.querySelector]



      This is my code...



      MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

      var observerjQueryPlugins = function (repeaterWrapper) {

      _.each(repeaterWrapper, function (repeaterItem, index) {

      var jq_nodes = $(repeaterItem.addedNodes);

      jq_nodes.each(function () {

      // Color Picker (Working Good)
      $(this).find('.element-wrapper.element-wpcolor .color-picker').wpColorPicker();

      // Switchery using document.querySelector and i need to know the
      // equivalent way to do it inside this loop .. like that

      // Of course this code is WRONG
      sw_current = $(this).find('.switchery-element');

      var switchery = new Switchery( sw_current, {
      disabled: false,
      size: '',
      color: '#8ce196',
      secondaryColor: '#ddd',
      jackColor: '#fff',
      jackSecondaryColor: '#fff'
      });

      });
      });

      }

      new MutationObserver(observerjQueryPlugins).observe(document.body, {
      childList: true,
      subtree: true,
      attributes: false,
      characterData: false
      });


      Thanks for your help.







      jquery-selectors mutation-observers switchery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 5 '16 at 13:24









      Hady Shaltout

      1121212




      1121212
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          It seems that Switchery is not a jQuery Plugin. That's why the sample code in GitHub uses document.querySelector to acquire element to apply it.



          As you use jQuery to find elements, you need to use jQuery.each in order to process every DOM element for Switchery.



          The following is an example to find element by jQuery and apply Switchery to each element. so, do the similar thing after sw_current = $(this).find('.switchery-element'); in your code.






          	$(function() {
          sw_current = $(this).find('.switchery-element');
          sw_current.each(function() {
          var switchery = new Switchery(this);
          })

          });

          @import url("http://abpetkov.github.io/switchery/dist/switchery.min.css");

          <input type="checkbox" class="switchery-element" checked />Check 1
          <p/>
          <input type="checkbox" class="switchery-element" checked />Check 2
          <p/>
          <input type="checkbox" class="switchery-element" checked />Check 3

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="http://abpetkov.github.io/switchery/dist/switchery.min.js"></script>








          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36427673%2fequivalent-document-queryselector-inside-mutation-observer-dynamic-elements%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            It seems that Switchery is not a jQuery Plugin. That's why the sample code in GitHub uses document.querySelector to acquire element to apply it.



            As you use jQuery to find elements, you need to use jQuery.each in order to process every DOM element for Switchery.



            The following is an example to find element by jQuery and apply Switchery to each element. so, do the similar thing after sw_current = $(this).find('.switchery-element'); in your code.






            	$(function() {
            sw_current = $(this).find('.switchery-element');
            sw_current.each(function() {
            var switchery = new Switchery(this);
            })

            });

            @import url("http://abpetkov.github.io/switchery/dist/switchery.min.css");

            <input type="checkbox" class="switchery-element" checked />Check 1
            <p/>
            <input type="checkbox" class="switchery-element" checked />Check 2
            <p/>
            <input type="checkbox" class="switchery-element" checked />Check 3

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script src="http://abpetkov.github.io/switchery/dist/switchery.min.js"></script>








            share|improve this answer

























              up vote
              0
              down vote













              It seems that Switchery is not a jQuery Plugin. That's why the sample code in GitHub uses document.querySelector to acquire element to apply it.



              As you use jQuery to find elements, you need to use jQuery.each in order to process every DOM element for Switchery.



              The following is an example to find element by jQuery and apply Switchery to each element. so, do the similar thing after sw_current = $(this).find('.switchery-element'); in your code.






              	$(function() {
              sw_current = $(this).find('.switchery-element');
              sw_current.each(function() {
              var switchery = new Switchery(this);
              })

              });

              @import url("http://abpetkov.github.io/switchery/dist/switchery.min.css");

              <input type="checkbox" class="switchery-element" checked />Check 1
              <p/>
              <input type="checkbox" class="switchery-element" checked />Check 2
              <p/>
              <input type="checkbox" class="switchery-element" checked />Check 3

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <script src="http://abpetkov.github.io/switchery/dist/switchery.min.js"></script>








              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                It seems that Switchery is not a jQuery Plugin. That's why the sample code in GitHub uses document.querySelector to acquire element to apply it.



                As you use jQuery to find elements, you need to use jQuery.each in order to process every DOM element for Switchery.



                The following is an example to find element by jQuery and apply Switchery to each element. so, do the similar thing after sw_current = $(this).find('.switchery-element'); in your code.






                	$(function() {
                sw_current = $(this).find('.switchery-element');
                sw_current.each(function() {
                var switchery = new Switchery(this);
                })

                });

                @import url("http://abpetkov.github.io/switchery/dist/switchery.min.css");

                <input type="checkbox" class="switchery-element" checked />Check 1
                <p/>
                <input type="checkbox" class="switchery-element" checked />Check 2
                <p/>
                <input type="checkbox" class="switchery-element" checked />Check 3

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="http://abpetkov.github.io/switchery/dist/switchery.min.js"></script>








                share|improve this answer












                It seems that Switchery is not a jQuery Plugin. That's why the sample code in GitHub uses document.querySelector to acquire element to apply it.



                As you use jQuery to find elements, you need to use jQuery.each in order to process every DOM element for Switchery.



                The following is an example to find element by jQuery and apply Switchery to each element. so, do the similar thing after sw_current = $(this).find('.switchery-element'); in your code.






                	$(function() {
                sw_current = $(this).find('.switchery-element');
                sw_current.each(function() {
                var switchery = new Switchery(this);
                })

                });

                @import url("http://abpetkov.github.io/switchery/dist/switchery.min.css");

                <input type="checkbox" class="switchery-element" checked />Check 1
                <p/>
                <input type="checkbox" class="switchery-element" checked />Check 2
                <p/>
                <input type="checkbox" class="switchery-element" checked />Check 3

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="http://abpetkov.github.io/switchery/dist/switchery.min.js"></script>








                	$(function() {
                sw_current = $(this).find('.switchery-element');
                sw_current.each(function() {
                var switchery = new Switchery(this);
                })

                });

                @import url("http://abpetkov.github.io/switchery/dist/switchery.min.css");

                <input type="checkbox" class="switchery-element" checked />Check 1
                <p/>
                <input type="checkbox" class="switchery-element" checked />Check 2
                <p/>
                <input type="checkbox" class="switchery-element" checked />Check 3

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="http://abpetkov.github.io/switchery/dist/switchery.min.js"></script>





                	$(function() {
                sw_current = $(this).find('.switchery-element');
                sw_current.each(function() {
                var switchery = new Switchery(this);
                })

                });

                @import url("http://abpetkov.github.io/switchery/dist/switchery.min.css");

                <input type="checkbox" class="switchery-element" checked />Check 1
                <p/>
                <input type="checkbox" class="switchery-element" checked />Check 2
                <p/>
                <input type="checkbox" class="switchery-element" checked />Check 3

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="http://abpetkov.github.io/switchery/dist/switchery.min.js"></script>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 at 0:45









                Yas Ikeda

                32518




                32518






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36427673%2fequivalent-document-queryselector-inside-mutation-observer-dynamic-elements%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to send String Array data to Server using php in android

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Is anime1.com a legal site for watching anime?