1

This works successfully,

$('[rel=notif]').on({
    mouseenter: function () {
$(this).find('.solnotifkara').css({"opacity":"0.46"});
  },
    mouseleave: function () {
$(this).find('.solnotifkara').css({"opacity":"0.36"});
    }
});

However, if its dynamically added, its not working. I have a click function that works for dynamically added elements,

$(document.body).on('click', '[rel="like"]' ,function(){

alert("works");

});

How can I make hover function work for dynamically added elements ?

3
  • $(document.body).on('hover', '[rel="notif"]'? Commented Sep 2, 2016 at 17:01
  • You need to use $(document) instead $(document.body). Commented Sep 2, 2016 at 17:01
  • @muhammed why ? Commented Sep 2, 2016 at 17:02

1 Answer 1

6
$(document).on({
  mouseenter: function () {
    $(this).find('.solnotifkara').css({opacity:0.46});
  },
  mouseleave: function () {
    $(this).find('.solnotifkara').css({opacity:0.36});
  }
}, "[rel=notif]");  // <<<<< delegate here your dynamic element

$(document).on({
  mouseenter: function () {
    $(this).find('.solnotifkara').css({opacity:0.2});
  },
  mouseleave: function () {
    $(this).find('.solnotifkara').css({opacity:1});
  }
}, "[rel=notif]");  // <<<<< delegate here your dynamic element


// for test only:
$("button").on("click", function(){
  $("body").append('<p><a rel="notif">This is a <span class="solnotifkara">note</span>.</a></p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>GENERATE</button>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I was curious when I saw you've changed the opacity but now I see that they're updated ;)
You're welcome! @user3304007 yes... I was just trying to avoid comments from other users like "hey this does not work" since the barely noticable opacity change :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.