Hover function not working on appended div content after append function












1















I am trying to add content with append function. But hover function not working on new div content after append. Can I get help about this?



jsfiddle code here : https://jsfiddle.net/xpwrv8mo/



JS CODE:



$('#add').click(function(){   
$('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
});

$(".temp").hover(function(){
$(this).find(".icon-del").show();
},function(){ $(this).find(".icon-del").hide(); });

$(".icon-del").click(function(event) {
if (!confirm('Are you sure?')) return false;
$(this).parent().remove();
return true;
});


HTML CODE:



    <div class="temp-wrapper">
<div class="temp"><div class="icon-del"></div><div>A</div></div>
<div class="temp"><div class="icon-del"></div><div>B</div></div>
<div class="temp"><div class="icon-del"></div><div>C</div></div>
</div>
<p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>









share|improve this question



























    1















    I am trying to add content with append function. But hover function not working on new div content after append. Can I get help about this?



    jsfiddle code here : https://jsfiddle.net/xpwrv8mo/



    JS CODE:



    $('#add').click(function(){   
    $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
    });

    $(".temp").hover(function(){
    $(this).find(".icon-del").show();
    },function(){ $(this).find(".icon-del").hide(); });

    $(".icon-del").click(function(event) {
    if (!confirm('Are you sure?')) return false;
    $(this).parent().remove();
    return true;
    });


    HTML CODE:



        <div class="temp-wrapper">
    <div class="temp"><div class="icon-del"></div><div>A</div></div>
    <div class="temp"><div class="icon-del"></div><div>B</div></div>
    <div class="temp"><div class="icon-del"></div><div>C</div></div>
    </div>
    <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>









    share|improve this question

























      1












      1








      1


      0






      I am trying to add content with append function. But hover function not working on new div content after append. Can I get help about this?



      jsfiddle code here : https://jsfiddle.net/xpwrv8mo/



      JS CODE:



      $('#add').click(function(){   
      $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
      });

      $(".temp").hover(function(){
      $(this).find(".icon-del").show();
      },function(){ $(this).find(".icon-del").hide(); });

      $(".icon-del").click(function(event) {
      if (!confirm('Are you sure?')) return false;
      $(this).parent().remove();
      return true;
      });


      HTML CODE:



          <div class="temp-wrapper">
      <div class="temp"><div class="icon-del"></div><div>A</div></div>
      <div class="temp"><div class="icon-del"></div><div>B</div></div>
      <div class="temp"><div class="icon-del"></div><div>C</div></div>
      </div>
      <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>









      share|improve this question














      I am trying to add content with append function. But hover function not working on new div content after append. Can I get help about this?



      jsfiddle code here : https://jsfiddle.net/xpwrv8mo/



      JS CODE:



      $('#add').click(function(){   
      $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
      });

      $(".temp").hover(function(){
      $(this).find(".icon-del").show();
      },function(){ $(this).find(".icon-del").hide(); });

      $(".icon-del").click(function(event) {
      if (!confirm('Are you sure?')) return false;
      $(this).parent().remove();
      return true;
      });


      HTML CODE:



          <div class="temp-wrapper">
      <div class="temp"><div class="icon-del"></div><div>A</div></div>
      <div class="temp"><div class="icon-del"></div><div>B</div></div>
      <div class="temp"><div class="icon-del"></div><div>C</div></div>
      </div>
      <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>






      jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 21:09









      webmasterwebmaster

      82




      82
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Use dynamic event triggers, if you use .click() they are only attached to elements that exist at that time. If you use the following code any click within an element with class .temp-wrapper is checked against the given selectors before running the associated function.



          $(".temp-wrapper").on("mouseenter", ".temp", function(){ ...  });

          $(".temp-wrapper").on("mouseleave", ".temp",function(){ ... });

          $(".temp-wrapper").on("click", ".icon-del", function(event) { ... });




          Demo






          $('#add').click(function() {
          $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
          });

          $(".temp-wrapper").on("mouseenter", ".temp", function() {
          $(this).find(".icon-del").show();
          });

          $(".temp-wrapper").on("mouseleave", ".temp", function() {
          $(this).find(".icon-del").hide();
          });

          $(".temp-wrapper").on("click", ".icon-del", function(event) {
          if (!confirm('Are you sure?')) return false;
          $(this).parent().remove();
          return true;
          });

          .icon-del {
          height: 10px;
          width: 10px;
          background: black;
          display: none;
          }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <div class="temp-wrapper">
          <div class="temp">
          <div class="icon-del"></div>
          <div>A</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>B</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>C</div>
          </div>
          </div>
          <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>








          share|improve this answer


























          • Thank you, now working perfectly. And thank you again for helpful info.

            – webmaster
            Jan 1 at 21:52













          • No problem, glad it helped.

            – Oliver Trampleasure
            Jan 1 at 22:01






          • 1





            Better and more efficient to listen on $('.temp-wrapper') rather than $(document) in this case.

            – Jeto
            Jan 1 at 23:16






          • 1





            Definitely - I've updated my answer, thanks for your input.

            – Oliver Trampleasure
            Jan 1 at 23:19











          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',
          autoActivateHeartbeat: false,
          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%2f53998959%2fhover-function-not-working-on-appended-div-content-after-append-function%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









          2














          Use dynamic event triggers, if you use .click() they are only attached to elements that exist at that time. If you use the following code any click within an element with class .temp-wrapper is checked against the given selectors before running the associated function.



          $(".temp-wrapper").on("mouseenter", ".temp", function(){ ...  });

          $(".temp-wrapper").on("mouseleave", ".temp",function(){ ... });

          $(".temp-wrapper").on("click", ".icon-del", function(event) { ... });




          Demo






          $('#add').click(function() {
          $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
          });

          $(".temp-wrapper").on("mouseenter", ".temp", function() {
          $(this).find(".icon-del").show();
          });

          $(".temp-wrapper").on("mouseleave", ".temp", function() {
          $(this).find(".icon-del").hide();
          });

          $(".temp-wrapper").on("click", ".icon-del", function(event) {
          if (!confirm('Are you sure?')) return false;
          $(this).parent().remove();
          return true;
          });

          .icon-del {
          height: 10px;
          width: 10px;
          background: black;
          display: none;
          }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <div class="temp-wrapper">
          <div class="temp">
          <div class="icon-del"></div>
          <div>A</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>B</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>C</div>
          </div>
          </div>
          <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>








          share|improve this answer


























          • Thank you, now working perfectly. And thank you again for helpful info.

            – webmaster
            Jan 1 at 21:52













          • No problem, glad it helped.

            – Oliver Trampleasure
            Jan 1 at 22:01






          • 1





            Better and more efficient to listen on $('.temp-wrapper') rather than $(document) in this case.

            – Jeto
            Jan 1 at 23:16






          • 1





            Definitely - I've updated my answer, thanks for your input.

            – Oliver Trampleasure
            Jan 1 at 23:19
















          2














          Use dynamic event triggers, if you use .click() they are only attached to elements that exist at that time. If you use the following code any click within an element with class .temp-wrapper is checked against the given selectors before running the associated function.



          $(".temp-wrapper").on("mouseenter", ".temp", function(){ ...  });

          $(".temp-wrapper").on("mouseleave", ".temp",function(){ ... });

          $(".temp-wrapper").on("click", ".icon-del", function(event) { ... });




          Demo






          $('#add').click(function() {
          $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
          });

          $(".temp-wrapper").on("mouseenter", ".temp", function() {
          $(this).find(".icon-del").show();
          });

          $(".temp-wrapper").on("mouseleave", ".temp", function() {
          $(this).find(".icon-del").hide();
          });

          $(".temp-wrapper").on("click", ".icon-del", function(event) {
          if (!confirm('Are you sure?')) return false;
          $(this).parent().remove();
          return true;
          });

          .icon-del {
          height: 10px;
          width: 10px;
          background: black;
          display: none;
          }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <div class="temp-wrapper">
          <div class="temp">
          <div class="icon-del"></div>
          <div>A</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>B</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>C</div>
          </div>
          </div>
          <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>








          share|improve this answer


























          • Thank you, now working perfectly. And thank you again for helpful info.

            – webmaster
            Jan 1 at 21:52













          • No problem, glad it helped.

            – Oliver Trampleasure
            Jan 1 at 22:01






          • 1





            Better and more efficient to listen on $('.temp-wrapper') rather than $(document) in this case.

            – Jeto
            Jan 1 at 23:16






          • 1





            Definitely - I've updated my answer, thanks for your input.

            – Oliver Trampleasure
            Jan 1 at 23:19














          2












          2








          2







          Use dynamic event triggers, if you use .click() they are only attached to elements that exist at that time. If you use the following code any click within an element with class .temp-wrapper is checked against the given selectors before running the associated function.



          $(".temp-wrapper").on("mouseenter", ".temp", function(){ ...  });

          $(".temp-wrapper").on("mouseleave", ".temp",function(){ ... });

          $(".temp-wrapper").on("click", ".icon-del", function(event) { ... });




          Demo






          $('#add').click(function() {
          $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
          });

          $(".temp-wrapper").on("mouseenter", ".temp", function() {
          $(this).find(".icon-del").show();
          });

          $(".temp-wrapper").on("mouseleave", ".temp", function() {
          $(this).find(".icon-del").hide();
          });

          $(".temp-wrapper").on("click", ".icon-del", function(event) {
          if (!confirm('Are you sure?')) return false;
          $(this).parent().remove();
          return true;
          });

          .icon-del {
          height: 10px;
          width: 10px;
          background: black;
          display: none;
          }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <div class="temp-wrapper">
          <div class="temp">
          <div class="icon-del"></div>
          <div>A</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>B</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>C</div>
          </div>
          </div>
          <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>








          share|improve this answer















          Use dynamic event triggers, if you use .click() they are only attached to elements that exist at that time. If you use the following code any click within an element with class .temp-wrapper is checked against the given selectors before running the associated function.



          $(".temp-wrapper").on("mouseenter", ".temp", function(){ ...  });

          $(".temp-wrapper").on("mouseleave", ".temp",function(){ ... });

          $(".temp-wrapper").on("click", ".icon-del", function(event) { ... });




          Demo






          $('#add').click(function() {
          $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
          });

          $(".temp-wrapper").on("mouseenter", ".temp", function() {
          $(this).find(".icon-del").show();
          });

          $(".temp-wrapper").on("mouseleave", ".temp", function() {
          $(this).find(".icon-del").hide();
          });

          $(".temp-wrapper").on("click", ".icon-del", function(event) {
          if (!confirm('Are you sure?')) return false;
          $(this).parent().remove();
          return true;
          });

          .icon-del {
          height: 10px;
          width: 10px;
          background: black;
          display: none;
          }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <div class="temp-wrapper">
          <div class="temp">
          <div class="icon-del"></div>
          <div>A</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>B</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>C</div>
          </div>
          </div>
          <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>








          $('#add').click(function() {
          $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
          });

          $(".temp-wrapper").on("mouseenter", ".temp", function() {
          $(this).find(".icon-del").show();
          });

          $(".temp-wrapper").on("mouseleave", ".temp", function() {
          $(this).find(".icon-del").hide();
          });

          $(".temp-wrapper").on("click", ".icon-del", function(event) {
          if (!confirm('Are you sure?')) return false;
          $(this).parent().remove();
          return true;
          });

          .icon-del {
          height: 10px;
          width: 10px;
          background: black;
          display: none;
          }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <div class="temp-wrapper">
          <div class="temp">
          <div class="icon-del"></div>
          <div>A</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>B</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>C</div>
          </div>
          </div>
          <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>





          $('#add').click(function() {
          $('.temp-wrapper').append('<div class="temp"><div class="icon-del"></div><div>new content</div></div>');
          });

          $(".temp-wrapper").on("mouseenter", ".temp", function() {
          $(this).find(".icon-del").show();
          });

          $(".temp-wrapper").on("mouseleave", ".temp", function() {
          $(this).find(".icon-del").hide();
          });

          $(".temp-wrapper").on("click", ".icon-del", function(event) {
          if (!confirm('Are you sure?')) return false;
          $(this).parent().remove();
          return true;
          });

          .icon-del {
          height: 10px;
          width: 10px;
          background: black;
          display: none;
          }

          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <div class="temp-wrapper">
          <div class="temp">
          <div class="icon-del"></div>
          <div>A</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>B</div>
          </div>
          <div class="temp">
          <div class="icon-del"></div>
          <div>C</div>
          </div>
          </div>
          <p style="text-align:center;"><input type="button" class="button" id="add" value="ADD" /></p>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 23:18

























          answered Jan 1 at 21:33









          Oliver TrampleasureOliver Trampleasure

          1,7361519




          1,7361519













          • Thank you, now working perfectly. And thank you again for helpful info.

            – webmaster
            Jan 1 at 21:52













          • No problem, glad it helped.

            – Oliver Trampleasure
            Jan 1 at 22:01






          • 1





            Better and more efficient to listen on $('.temp-wrapper') rather than $(document) in this case.

            – Jeto
            Jan 1 at 23:16






          • 1





            Definitely - I've updated my answer, thanks for your input.

            – Oliver Trampleasure
            Jan 1 at 23:19



















          • Thank you, now working perfectly. And thank you again for helpful info.

            – webmaster
            Jan 1 at 21:52













          • No problem, glad it helped.

            – Oliver Trampleasure
            Jan 1 at 22:01






          • 1





            Better and more efficient to listen on $('.temp-wrapper') rather than $(document) in this case.

            – Jeto
            Jan 1 at 23:16






          • 1





            Definitely - I've updated my answer, thanks for your input.

            – Oliver Trampleasure
            Jan 1 at 23:19

















          Thank you, now working perfectly. And thank you again for helpful info.

          – webmaster
          Jan 1 at 21:52







          Thank you, now working perfectly. And thank you again for helpful info.

          – webmaster
          Jan 1 at 21:52















          No problem, glad it helped.

          – Oliver Trampleasure
          Jan 1 at 22:01





          No problem, glad it helped.

          – Oliver Trampleasure
          Jan 1 at 22:01




          1




          1





          Better and more efficient to listen on $('.temp-wrapper') rather than $(document) in this case.

          – Jeto
          Jan 1 at 23:16





          Better and more efficient to listen on $('.temp-wrapper') rather than $(document) in this case.

          – Jeto
          Jan 1 at 23:16




          1




          1





          Definitely - I've updated my answer, thanks for your input.

          – Oliver Trampleasure
          Jan 1 at 23:19





          Definitely - I've updated my answer, thanks for your input.

          – Oliver Trampleasure
          Jan 1 at 23:19




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998959%2fhover-function-not-working-on-appended-div-content-after-append-function%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

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules